【问题标题】:Updating the end of the file in c++ fstream在c ++ fstream中更新文件的结尾
【发布时间】:2013-05-21 19:34:16
【问题描述】:

我写了这段代码:

#include <fstream>
#include <iostream>

using namespace std;

struct Man
{
    int  ID;
    char Name[20];
};

void Add();
void Update();
void Print();

int main()
{
    int n;

    cout << "1-add, 2-update, 3-print, 5-exit" << endl;
    cin >> n;

    while (n != 5)
    {
        switch (n)
        {
            case 1: Add(); break;
            case 2: Update(); break;
            case 3: Print(); break;
        }

        cout << "1-add, 2-update, 3-print, 5-exit" << endl;
        cin >> n;
    }
    return 0;
}

void Add()
{
    fstream file;

    file.open("Data.dat", ios::in | ios::out | ios::binary);

    if (file.is_open())
    {
        int  id;
        Man  man;
        bool didFound = false;

        cout << "ID  : ";
        cin >> id;

        file.read((char*)&man, sizeof(Man));

        while (!file.eof() && !didFound)
        {
            if (man.ID == id)
            {
                cout << "Already exist" << endl;
                didFound = true;
            }

            file.read((char*)&man, sizeof(Man));
        }

        if (!didFound)
        {
            man.ID = id;
            cout << "Name: ";
            cin >> man.Name;

            file.clear();
            file.seekp(0, ios::end);
            file.write((char*)&man, sizeof(Man));
        }
    }
}

void Update()
{
    fstream file;

    file.open("Data.dat", ios::in | ios::out | ios::binary);

    if (file.is_open())
    {
        int  id;
        Man  man;
        bool didFound = false;

        cout << "ID  : ";
        cin >> id;

        file.read((char*)&man, sizeof(Man));

        while (!file.eof() && !didFound)
        {
            if (man.ID == id)
            {
                cout << "Name: ";
                cin >> man.Name;

                file.seekp((int)file.tellg() - sizeof(Man), ios::beg);
                file.write((char*)&man, sizeof(Man));

                didFound = true;
            }

            file.read((char*)&man, sizeof(Man));
        }

        file.close();

        if (!didFound)
        {
            cout << "Cant update none existing man" << endl;
        }
    }
}

void Print()
{
    fstream file;

    file.open("Data.dat", ios::in | ios::binary);

    if (file.is_open())
    {
        int  id;
        Man  man;
        bool didFound = false;

        cout << "ID\tName" << endl;

        file.read((char*)&man, sizeof(Man));

        while (!file.eof())
        {
            cout << man.ID << '\t' << man.Name << endl;

            file.read((char*)&man, sizeof(Man));
        }

        file.close();
    }
}

但我在更新功能中有一个问题: 当我更新文件中的最后一个 Man 时,当它到达 file.read 时,文件会将最后一个 Man 的值(在写入之前的文件中)写入文件末尾(在更新后的 man 之后)

我在 file.write 之后添加了这个,它似乎解决了它:

file.seekg(file.tellp(), ios::beg);

谁能解释一下原因?

(是的,可以用else解决)

【问题讨论】:

    标签: c++ fstream


    【解决方案1】:

    有点随意,你需要在readwrite之间执行seek。标准中没有说明,但 C++ 标准提到 C++ fstream 与 C stdio 流在流操作的有效性方面具有相同的属性,并且 C 标准提到在读取之间需要定位命令和写作(反之亦然)。

    一些平台放宽了要求。 GCC大概在4.5或者4.6版本之后,我个人修改了basic_filebuf以消除拜占庭规则。

    顺便说一句,file.seekg( 0, ios::cur ) 更安全。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      • 1970-01-01
      • 2012-04-05
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多