在fstream的帮助下从文件中读取数据后,代码中出现错误



在创建一个简单的库存管理系统时,在列表中添加新项目后遇到了一些问题。我的代码会更好地解释。

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std; // For sake of speed
class INVENTORY
{
char name[10];
int code;
float cost;
public:
void setData();
void getData();
};
void INVENTORY ::setData()
{
std::cout << "Enter Name: ";
std::cin.getline(name, 10);
std::cout << "Enter Code: ";
std::cin >> code;
std::cout << "Enter Cost: ";
std::cin >> cost;
}
void INVENTORY::getData()
{
cout << setiosflags(ios::left) << setw(10) << name << setw(10) << code << setw(5) << cost << 'n';
}
int main()
{
INVENTORY item;
fstream inoutfile;
inoutfile.open("STOCK.DAT", ios::ate | ios::in | ios::out | ios::binary);
inoutfile.seekg(0, ios::beg); // go to beginning.
cout << "CURRENT CONTENTS OF STOCK" << 'n';
while (inoutfile.read((char *)&item, sizeof(item)))
{
item.getData();
}
inoutfile.clear(); // clears the eof state flag.
cout<<'n'<<inoutfile.good()<<'n';
/* ADD more items */
cout << "nAdd an item: n";
item.setData();
char ch;
cin.get(ch);
inoutfile.write((char *)&item, sizeof(item));
cout<<'n'<<inoutfile.good()<<'n';
// Display the appended file
inoutfile.seekg(0);
cout << "CONTENTS OF APPENDED FILEn";
while (inoutfile.read((char *)&item, sizeof(item)))
{
item.getData();
}
cout<<'n'<<inoutfile.good()<<'n';
// Find the number of objects in the file.
int last = inoutfile.tellg();
int n = last / sizeof(item);
cout << "Number of Objects: " << n << endl;
cout << "Total bytes in file: " << last << endl;
/* Modify the details of an item */
cout << "Enter object number to be updated: ";
int object;
cin >> object;
cin.get(ch);
int location = (object - 1) * sizeof(item);
if (inoutfile.eof())
{
inoutfile.clear();
}
inoutfile.seekp(location);
cout << "Enter the new values of objects n";
item.setData();
cin.get(ch);
inoutfile.write((char *)&item, sizeof item) << flush;
/* SHOW UPDATED FILE */
cout << "Contents of updated file: n";
while (inoutfile.read((char *)&item, sizeof item))
{
item.getData();
}
inoutfile.close();
return 0;
}

我重用了一些文件中的类,请不要因为using namespace std而起诉我。我通常不使用它,但为了速度,今天使用了它。

第三个cout<<'n'<<inoutfile.good()<<'n';返回false,我不知道为什么会发生这种情况。我已经有了文件STOCK.DAT,并且其中已经存在(相同类型的(数据。相关输出:

CURRENT CONTENTS OF STOCK
Apple     5         50   
Banana    6         80   
1
Add an item: 
Enter Name: Pineapple
Enter Code: 8
Enter Cost: 150
1
CONTENTS OF APPENDED FILE
Apple     5         50   
Banana    6         80   
Pineapple 8         150  
0 // something is not good here but what?
Number of Objects: -858993460
Total bytes in file: -1

输出中有更多的元素,但我向您展示了相关的输出,tellg返回false,所以肯定有问题,我无法解决。

我会解释一下这里发生了什么:

  • 我创建了一个名为INVENTORY的类,并在其中创建了一些成员和成员函数
  • 然后在主函数中,我创建了一个fstream对象,并打开了一个名为STOCK.DAT的文件,其中包含一些额外的标志
  • 然后我将get指针指向开始(对于一个fstream对象,两个指针一起移动(
  • 该文件是二进制的,所以我打印出文件中已经存在的内容
  • 然后我使用clear()来删除eof标志,并检查是否一切正常,这将在这里得到true
  • 然后我在文件的末尾添加另一项,检查是否一切都好,这里也是正确的
  • 然后我将get指针设置为开始再次打印文件中的所有数据,现在所有数据都不是good,我无法弄清楚

我找到了一个解决方案。在到达eof之后,如果您尝试执行inoutfile.tellg(),它将返回-1。而是使用inoutfile.clear()来清除eof标记,然后使用inoutfile.tellg()

最新更新