C++:.eof在空文件上

Moh*_*bil 4 c++ fstream eof

让我们看看这个程序:

ifstream filein("hey.txt");


if(filein.eof()){
    cout<<"END"<<endl;
}
Run Code Online (Sandbox Code Playgroud)

这里"hey.txt"是空的.所以这里的if条件被认为应该是真的但它不是

尽管文件为空,为什么eof不返回true?

如果我if在eof返回true 之前添加了这个,尽管arr仍然是空的并且文件仍为空,因此两者都保持不变

char arr[100];
filein.getline(arr,99);
Run Code Online (Sandbox Code Playgroud)

Yip*_*Yay 5

eof() 程序尝试读取文件末尾后,函数返回"true".

您可以std::ifstream::peek()用来检查"逻辑文件结尾".