借助fstream从文件中读取数据后代码出错

Error in code after reading data from file with the help of 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 FILE\n";
    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;
}

我重复使用了某个文件中的class,请不要起诉我using namespace std我通常不使用它,但为了速度今天使用了

第三个 cout<<'\n'<<inoutfile.good()<<'\n'; returns 错误,我无法弄清楚为什么会这样。我已经有了文件 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 returns false,所以肯定有问题,我想不通。

我会解释这里发生的事情:

我找到了解决办法。达到 eof 后,如果您尝试 inoutfile.tellg(),它将 return -1。而是使用 inoutfile.clear() 清除 eof 标签,然后使用 inoutfile.tellg().