我试图使用eof和peek,但似乎两者都没有给我正确的答案。
if (inputFile.fail()) //check for file open failure
{
cout << "Error opening file" << endl;
cout << "Note that the program will halt" << endl;//error prompt
}
else if (inputFile.eof())
{
cout << "File is empty" << endl;
cout << "Note that program will halt" << endl; // error prompt
}
else
{
//run the file
}
使用这种方法无法检测到任何空文件。如果我使用 inputFile.peek 而不是 eof,那么它会将我的好文件误判为空文件。
peek()
的?顺便说一下,EOF标志仅在读取到文件末尾后设置。 - Ry-