我遇到了这段代码,它处理文件的简单读取并显示其内容。
#include <iostream>
#include <fstream>
int main()
{
...
fstream file;
file.open("TEXT.txt", ios::in);
file.seekg(0);
while(file) //does file returns 0 when eof is reached?
{
file.get(ch);
cout << ch;
}
return 0;
}
我的问题是 while (file)
如何意识到已到达文件末尾。
最佳答案
该流实现了 boolean cast operator如果流仍然良好,则返回 true;如果出现错误或 eof,则返回 false。
关于c++ - fstream 对象的文件结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32930715/