我目前正在尝试编写程序的一部分,将数组中的对象读入文本文件,反之亦然。我可以将对象输出到一个似乎没有问题的文件,但是当我尝试将文本文件中的数据读入一个空数组时,它将最后一个对象放在第一个对应的位置,并将所有其他对象留空。我哪里出错?
这是我的班级代码
//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)
{
while (fileIn >> stockCode >> stockDesc >> currentLevel >> reorderLevel)
{
fileIn >> stockCode;
fileIn >> stockDesc;
fileIn >> currentLevel;
fileIn >> reorderLevel;
}
}
这是我在主要
中的代码#include <iostream>
#include <string>
#include <fstream>
#include "Stock.h"
using namespace std;
int main()
{
Stock items[4];
int option = 0;
cout << "1.Display full stock list." << endl;
cout << "Please pick an option: ";
cin >> option;
switch (option)
{
case 1:
cout << "stockCode" << '\t' << "stockDesc" << '\t' << '\t' << "CurrentLevel" << '\t' << "ReorderLevel" << endl;
cout << "------------------------------------------------------------------------------" << endl;
ifstream fileIn;
fileIn.open("Stock.txt");
for (int i = 0; i < 4; i++)
{
items[i].readFromFile(fileIn);
cout << items[i].getCode() << '\t' << '\t';
cout << items[i].getDescription() << '\t' << '\t' << '\t';
cout << items[i].getCurrentLevel() << '\t' << '\t';
cout << items[i].getReorderLevel() << endl;
}
}
return 0;
}
答案 0 :(得分:1)
这个循环遍历整个文件,直到它不能再读取为止,这就是最后一组变量只有一个可见的原因。所有先前的都被覆盖。
while (fileIn >> stockCode >> stockDesc >> currentLevel >> reorderLevel)
{
fileIn >> stockCode;
fileIn >> stockDesc;
fileIn >> currentLevel;
fileIn >> reorderLevel;
}
第一次进入你的for循环,调用
items[i].readFromFile(fileIn);
遍历整个文件。 for循环中的所有剩余迭代,尝试从文件中读取,但它已经在EOF。
答案 1 :(得分:1)
正如molbdnilo所指出的那样,你想要从文件中顺序读取你的库存对象,所以应该删除循环。同样在这种情况下,最好使用自定义友元函数直接从流中读取和写入对象。请参阅下面的代码,以实现这一目标
#include <iostream>
#include <fstream>
#include <string>
// This what goes into your "Stock.h"
class Stock{
std::string stockCode;
std::string stockDesc;
int currentLevel;
int reorderLevel;
public:
Stock():currentLevel(0),reorderLevel(0){};
Stock(std::string const & scode,
std::string const & sdesc,
int const clevel,
int const rlevel
):stockCode(scode),
stockDesc(sdesc),
currentLevel(clevel),
reorderLevel(rlevel){}
friend std::istream& operator >>(std::istream& is, Stock& stk)
{
if (is) {
is
>> stk.stockCode
>> stk.stockDesc
>> stk.currentLevel
>> stk.reorderLevel;
}
return is;
}
friend std::ostream& operator <<(std::ostream& os, Stock const& stk)
{
os
<< stk.stockCode << ' '
<< stk.stockDesc << ' '
<< stk.currentLevel << ' '
<< stk.reorderLevel
<< '\n' //Line break after every object so that you can open and read.
;
}
};
//This is your main code with the read loop fixed.
int main()
{
const int N = 4;
Stock items[N];
//1. Create 4 Stock objects.
items[0] = Stock("A", "STKA", 100, 100);
items[1] = Stock("B", "STKB", 101, 101);
items[2] = Stock("C", "STKC", 102, 102);
items[3] = Stock("D", "STKD", 103, 103);
//2. Write the 4 Stock objects to a file.
std::ofstream ofs;
ofs.open("Stock.txt", std::ofstream::out);
for ( int i = 0; i < N ; ++i ) {
ofs << items[i] ;
}
ofs.close();
// 3. Read from the file written in 2. and print.
std::ifstream fileIn;
fileIn.open("Stock.txt");
Stock stk;
while (fileIn >> stk) {
std::cout << stk;
}
fileIn.close();
}
输出结果为:
STKA 100 100
B STKB 101 101
C STKC 102 102
D STKD 103 103
答案 2 :(得分:0)
您正在将整个文件读入第一个Stock
,您甚至会阅读每个Stock
的数据两次。
剩下的股票因为你已经到达文件的末尾而失败了。
从readFromFile
删除循环。
答案 3 :(得分:0)
对于每个项目,您正在循环,直到Stock::readFromFile()
中到达文件末尾。你需要在那里删除循环:
//Defining function for reading items in from the file
void Stock::readFromFile(ifstream& fileIn)
{
fileIn >> stockCode;
fileIn >> stockDesc;
fileIn >> currentLevel;
fileIn >> reorderLevel;
}
所以,剩下的物品没有什么可读的了。