我正在尝试使用C++进行try,catch,throw语句处理文件,并且我编写了一个虚拟代码来捕获所有错误.我的问题是为了检查我是否有这些权利,我需要发生错误.现在,我可以infile.fail()通过简单地不在目录中创建所需名称的文件来轻松检查.但是,我怎么就能检查同为outfile.fail()(outfile是ofstream这里的infile是ifstream).在哪种情况下,值outfile.fail()是真的吗?
示例代码[来自对unapersson的回答的评论,简化为使问题更清晰 - -zack]:
#include <fstream>
using std::ofstream;
int main()
{
ofstream outfile;
outfile.open("test.txt");
if (outfile.fail())
// do something......
else
// do something else.....
return 0;
}
Run Code Online (Sandbox Code Playgroud) c++ ×1