让我们看看这个程序:
ifstream filein("hey.txt");
if(filein.eof()){
cout<<"END"<<endl;
}
这里“hey.txt”是空的。所以这里的 if 条件被认为应该是真的但是它不是
为什么文件是空的但eof不返回true?
如果我在 if
之前添加这个 eof 返回 true 尽管 arr
仍然是空的并且文件仍然是空的所以两者都没有变化
char arr[100];
filein.getline(arr,99);
最佳答案
eof()
function returns "true" after the program attempts to read past the end of the file.
您可以使用 std::ifstream::peek()
检查“逻辑文件结束”。
关于C++: .eof 在一个空文件上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12134242/