我正在尝试使用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
如下
if ( inputFile.peek() == std::ifstream::traits_type::eof() )
{
// Empty File
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句