比如
ofstream f;
f.open("123.txt");
此时如果123.txt不存在,则编译器自动创建一个123.txt的空文件,用
if (!f)
cout<<"Error";
是检测不出来的,而新的标准库也不支持ios::nocreate了
应该如何检查该文件是否成功打开呢?
1、可以测试是否能作为可读文件打开,例如:
ifstream fin("123.txt")
if(!fin) cout<<"Error!"<<endl;
else { fin.close(); ofstream fout("123.txt"); }
2、例程:
#include <iostream>