【发布时间】:2019-07-28 09:56:00
【问题描述】:
在下面的代码中,如果文件为空,我会尝试打印消息,然后抛出异常。考虑文件 Text.txt 是空的。
ifstream inputFile;
try
{
inputFile.exceptions(ifstream::eofbit);
inputFile.open("Text.txt");
if (inputFile.is_open()) {
cout << "file opened"<<endl;
}
if (inputFile.peek()== ifstream::traits_type::eof())
{
cout << "file opened but it is empty or invalid
content" << endl;
}
}
catch (ifstream::failure &e)
{
cout << "Some issue with input file: " << e.what()<<endl;
_exit(1);
}
但它在进入 if 块之前抛出异常(if (inputFile.peek()== ifstream::traits_type::eof()))。我错过了什么?
【问题讨论】:
-
异常错误说明了什么?请将错误添加到您的原始帖子中。