C++ 将对象从文件读入数组,反之亦然

C++ 将对象从文件读入数组,反之亦然,c++,arrays,file,oop,object,C++,Arrays,File,Oop,Object,我目前正在尝试编写一个程序,可以将文件中的对象读取到数组中。在程序结束时,我希望它将数组的内容写入文件中。到目前为止,我已经在这方面取得了一定程度的成功,我的从文件读取方法似乎没有任何问题,在某种程度上我知道我已经接近于我的从文件写入方法。它可以工作,但也输出默认构造函数生成的数组新元素。有什么方法可以阻止这些默认对象被写入文件,或者更好的是,首先阻止它们被创建 下面是我的成员变量、默认构造函数和类中的方法 private: //Member variables string s

我目前正在尝试编写一个程序,可以将文件中的对象读取到数组中。在程序结束时,我希望它将数组的内容写入文件中。到目前为止,我已经在这方面取得了一定程度的成功,我的从文件读取方法似乎没有任何问题,在某种程度上我知道我已经接近于我的从文件写入方法。它可以工作,但也输出默认构造函数生成的数组新元素。有什么方法可以阻止这些默认对象被写入文件,或者更好的是,首先阻止它们被创建

下面是我的成员变量、默认构造函数和类中的方法

private:
    //Member variables
    string stockCode;
    string stockDesc;
    int currentLevel;
    int reorderLevel;

//Defining Default Constructor
Stock::Stock()
{

}

//Defining function for items to file
void Stock::writeToFile(ofstream& fileOut)
{
    fileOut << stockCode << " ";
    fileOut << stockDesc << " ";
    fileOut << currentLevel << " ";
    fileOut << reorderLevel << " ";
}

//Defining function for reading items in from the file
void Stock::readFromFile(ifstream& fileIn)
{
        fileIn >> stockCode;
        fileIn >> stockDesc;
        fileIn >> currentLevel;
        fileIn >> reorderLevel;
}
private:
//成员变量
字符串股票代码;
字符串stockDesc;
int电流级;
int-reordlevel;
//定义默认构造函数
股票:股票
{
}
//为要归档的项目定义函数
作废库存::writeToFile(流和文件输出)
{
文件输出重新排序级别;
}
这是我的主要任务

#include <iostream>
#include <string>
#include <fstream>
#include "Stock.h"

using namespace std;

int main()
{   
    const int N = 15;
    Stock items[N];
    int option = 0;
    ifstream fileIn;
    fileIn.open("Stock.txt");
    for (int i = 0; i < N; ++i)
        items[i].readFromFile(fileIn);
    fileIn.close();
    cout << "1.Display full stock list." << endl;
    cout << "9.Quit." << endl;
    cout << "Please pick an option: ";
    cin >> option;
    switch (option)
    {
    case 1:
    {
        cout << "stockCode" << '\t' << "stockDesc" << '\t' << '\t' << "CurrentLevel" << '\t' << "ReorderLevel" << endl;
        cout << "------------------------------------------------------------------------------" << endl;
        for (int i = 0; i < N; ++i)
        {
            cout << items[i].getCode() << '\t' << '\t';
            cout << items[i].getDescription() << '\t' << '\t' << '\t';
            cout << items[i].getCurrentLevel() << '\t' << '\t';
            cout << items[i].getReorderLevel() << endl;
        }
        break;
    }

    case 9:
        ofstream fileOut;
        fileOut.open("Stock.txt");
        for (int i = 0; i < N; ++i)
        {
            items[i].writeToFile(fileOut);
        }
        break;
    }
    return 0;
}
#包括
#包括
#包括
#包括“Stock.h”
使用名称空间std;
int main()
{   
常数int N=15;
库存项目[N];
int选项=0;
ifstreamfilein;
fileIn.open(“Stock.txt”);
对于(int i=0;icout您可以跟踪文件中存储的项目数,并通过将该数字存储为文件中的第一个值,最终添加到代码中:

int num_items = 0;
// keep track of number of items in code
// when writing to the file, first write num_items
fileOut << num_items << " ";

然后,不用数组,而是使用数组来存储和添加元素(您可以预先将其大小设置为
num\u items
,以防止使用另一个变量重新分配)。

首先,最好使用
操作符>
操作符跟踪您有多少个元素。
库存项目[N]
最好使用
std::vector items;
并使用
push_back()
填充它。
fileIn >> num_items;