简体   繁体   English

使用ofstream将数据写入文件后更新ifstream对象

[英]Update ifstream object after data is written to its file using ofstream

While testing my code, I faced an issue, that ifstream doesn't get updated when more data is written to its file. 在测试代​​码时,我遇到一个问题,即当有更多数据写入其文件时, ifstream不会得到更新。 So here is a sample code that demonstrates the problem: 因此,以下是示例代码演示了此问题:

    ifstream is(filename);
    string line;
    while (getline(is, line))
        cout << "line: " << line << endl;

    ofstream os(filename, ofstream::out | ofstream::app);
    string additional("additional");
    os << additional;
    os.flush();

    while (getline(is, line))
        cout << "line additional: " << line << endl;

No additional lines were written to stdout , though they are written to the file. 尽管其他行已写入文件, stdout其他行写入stdout I'm not using fstream instead of a couple of if/ofstream because I need it like this for testing purposes. 我没有使用fstream而不是几个if / ofstream,因为出于测试目的,我需要像这样使用它。

How to make ifstream "see" the changes in the file? 如何使ifstream “查看”文件中的更改?

UPDATE : I cleared the bits using clear method. 更新 :我使用clear方法清除了位。 It works OK on my Ubuntu machine with gcc. 它可以在我的带有gcc的Ubuntu计算机上正常运行。 But it doesn't work on my Mac OSX with llvm. 但是在带有llvm的Mac OSX上它不起作用。 Do you know how to do it platform independently? 你知道如何独立地做它吗?

You need to call std::ios::clear on the input stream after the first read. 第一次读取后,您需要在输入流上调用std::ios::clear When you read the whole file, it sets the failbit in the stream and will refuse to keep reading, even if the file actually changed in the meantime. 当您读取整个文件时,即使在此期间实际更改了文件,它也会在流中设置故障位,并且将拒绝继续读取。

ifstream is(filename);
string line;
while (getline(is, line))
    cout << "line: " << line << endl;

ofstream os(filename, ofstream::out | ofstream::app);
string additional("additional");
os << additional;
os.flush();

is.clear(); //< Now we can read again
while (getline(is, line))
    cout << "line additional: " << line << endl;

When you hit the end of the file while reading is the first time, it sets the eofbit in the stream's internal error state. 当你点击文件的末尾,而阅读is第一次,这台eofbit在流的内部错误状态。

You need to clear it before you can continuing reading, by calling the is.clear() function, which resets the internal error state. 您需要通过调用is.clear()函数来清除它,然后才能继续阅读,该函数将重置内部错误状态。

I think the reason is that you have opened output stream file using "ofstream::out" 我认为原因是您使用“ ofstream :: out”打开了输出流文件

ofstream os(filename, ofstream::out | ofstream::app);

now, the "ofstream::out" truncates the file as soon as it opens it. 现在,“ ofstream :: out”一旦打开文件就会将其截断。 That is, all the previous contents of your file will be deleted once it is opened. 也就是说,文件的所有先前内容一旦打开就将被删除。 Please try using this instead: 请尝试使用此代替:

ofstream os(filename, ios::app);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的代码不会使用 ifstream 和 ofstream 从文件中读取和显示数据 - My code will not read and display data from a file using ifstream and ofstream 创建一个打开文件并使用 ifstream 和 ofstream 读取它的函数,但它的 QT 说我没有声明该函数 - Creating a function that opens a file and reads it using ifstream and ofstream but its QT says i didnt declare the function 崩溃后ifstream和ofstream无法正常工作 - ifstream and ofstream not working after crash ifstream和ofstream或fstream使用in和out - ifstream and ofstream or fstream using in and out 使用 ifstream 和 ofstream 与 cin 和 cout 的区别 - Difference between using ifstream and ofstream with cin and cout 如何在 a.txt 文件中流式传输数值并在同一程序中通过 ifstream 读取相同的数据 - How to ofstream a numeric value in a .txt file and read same data through ifstream in same program 到达文件末尾后倒带 ifstream 对象 - Rewind an ifstream object after hitting the end of file 使用 ifstream 读取.txt 并使用 ofstream 写入 new.txt 文件,但只有第一行有效 - Reading .txt using ifstream and writing to new .txt file with ofstream, but only first line works 使用ofstream而不是ifstream和fstream /创建的文件,似乎无法访问gcount() - File created when using ofstream, but not ifstream and fstream/cannot seem to access gcount() 是否有 ifstream 和 ofstream 的宏或缩短 ifstream/ofstream 的方法? - Is there a macro for ifstream and ofstream or a way to shorten ifstream/ofstream?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM