求了解ifstream 求解我的ifstream后面跟路径怎么没用啊ifstreamifs( D:\a.txt )if(ifsfalse){cerr
求了解ifstream 求解
我的ifstream后面跟路径 怎么没用啊
ifstream ifs( "D:\a.txt ");
if (ifs == false) {
cerr < < "Unable to open file " < < endl;
return -1;
}
这个文件存在
但是我的结果 Unable to open file
有什么好的方法解决
[解决办法]
If the constructor is not successful in opening the file, the object is still created although no file is associated to the stream buffer and the stream's failbit is set (which can be checked with inherited member fail).
用成员函数fail()
if (ifs.fail())
一个输出例子
- C/C++ code
// ifstream constructor.#include <iostream>#include <fstream>using namespace std;int main () { ifstream ifs ( "test.txt" , ifstream::in ); while (ifs.good()) cout << (char) ifs.get(); ifs.close(); return 0;} 