首页 > 解决方案 > 在构造函数中创建二进制文件

问题描述

我写了一个Store包含Item. 我创建了两个函数,一是打印文件的内容,一是对记录进行排序并将它们插入回文件中。使用初始化列表,它可以正确显示文件的内容,但说有“文件错误”。如何更正此问题以及在构造函数中创建二进制文件的最佳方法是什么

#pragma once
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

struct Item
{
    char name[20];
    int code;
    float price;
    int quantity;

    Item() { code = 0, price = 0, quantity = 0;
    char name1[20] = { 0 }; strcpy_s(name, 20, name1);
    }
    Item(const char* name, int code, float price, int quantity)
    {
        strcpy_s(this->name, strlen(name) + 1, name);
        this->code = code;
        this->price = price;
        this->quantity = quantity;
    }
    friend ostream& operator<<(ostream& os, Item& item)
    {
        os << "Name: " << item.name << endl;
        os << "code: " << item.code << endl;
        os << "price: " << item.price << endl;
        os << "quantity: " << item.quantity << endl << endl;
        return os;
    }
    bool operator!()
    {
        return code != 0;
    }
};
class Store
{
private:
    int size = 100;
    fstream file;
    string fileName;
public:
    Store(string fname);
    ~Store();
    Store& operator+=(Item& myItem);
    friend ostream& operator<<(ostream&, Store&);
    void sortStore();
};
#include "Store.h"
Store::Store(string fname) : file(fname,ios::binary)
    {
    
        Item tmp;
        if (!file)
            cout << "Error with file" << endl;
        for (int i = 0; i < size; i++)
            file.write((char*)&tmp, sizeof(Item));
        //file.close();
        file.open(fname, ios::binary | ios::in | ios::out);
    }
Store::~Store()
{
    file.close();
}
Store& Store::operator+=(Item& myItem)
{
    file.write((char*)&myItem, sizeof(myItem));
    return (*this);
}
ostream& operator<<(ostream& out, Store& store)
{
    Item tmp;
    store.file.seekg(0, ios::beg);
    for (int i = 0; i < store.size; i++)
    {
        store.file.read((char*)&tmp, sizeof(Item));
        if (!tmp)
            cout << i << ": " <<  tmp;
    }
    return out;
}
void Store::sortStore()
{
    Item tmp;
    vector<Item> items;
    file.seekg(0, ios::beg);
    while (!file.eof())
    {
        file.read((char*)&tmp, sizeof(Item));
        items.push_back(tmp);
    }
    sort(items.begin(), items.end(), [](Item& first, Item& second)
        {
            return first.code < second.code;
        });

    file.clear();
    remove("store.dat");
    Store("store.dat");

    file.seekp(0, ios::beg);
    
    for (auto it = items.begin(); it < items.end(); it++)
        file.write((char*)&(*it), sizeof(Item));
}

int main()
{
    Store ourStore("store.dat");
    Item apple("apple", 45, 12.2, 10);
    Item ananas("ananas", 12, 27, 10);
    Item strawberry("strawberry", 1, 24.2, 5);
    Item banana("banana",23,14.2, 12);
    ourStore.operator+=(apple);
    ourStore.operator+=(ananas);
    ourStore.operator+=(strawberry);
    ourStore.operator+=(banana);

    int choice = -1;
    while (choice != 0)
    {
        cout << "1.Sorting, 2 Print" << endl;
        switch (choice)
        {
        case 1:
            cout << "Sorting" << endl;
            ourStore.sortStore();
        case 2:
            cout << "Print" << endl;
            cout << ourStore << endl;
        }
        cin >> choice;
    }



    return 0;
}

标签: c++fileclass

解决方案


file中的局部变量Store::Store隐藏了成员变量Store::file

你想要一个初始化列表:

Store::Store : file(fname, ios::binary)
{
// ...

推荐阅读