【问题标题】:Reading objects into array from file and vice versa将对象从文件读入数组,反之亦然
【发布时间】:2016-04-22 16:51:14
【问题描述】:

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

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

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;
}

这是我的主要内容

#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 items[N]; 最好使用std::vector&lt;Stock&gt; items; 并使用push_back() 来填充它。

标签: c++ arrays file oop object


【解决方案1】:

您可以跟踪存储在文件中的项目数量,并最终通过将该数字存储为文件中的第一个值来添加到代码中:

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_items:

fileIn >> num_items;

然后,使用std::vector 来存储和添加元素,而不是数组(您可以使用reserve() 预先将其大小设置为num_items 以防止重新分配)。

【讨论】:

    【解决方案2】:

    首先,最好将overloadoperator &gt;&gt;operator &lt;&lt;istreamostream 一起使用,而不是函数:writeToFile()readFromFile()

    其次,您可以使用std::vector&lt;Stock&gt; 而不是数组Stock items[N];,并且您的读写循环会更简单,只涉及一个while 循环从文件中读取一行,可能还有一个stringstream 提取该行的数据成员(stockCodestockDesc 等)。此外,将不再需要 static 变量 N,因为您将向量 size() 作为元素数量的指标。

    您可以先看看std::vectorstreams 以了解如何实现上述建议的代码,并辅以一些额外的研究。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 2017-11-02
      相关资源
      最近更新 更多