首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

求了解ifstream 求解解决思路

2012-05-24 
求了解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;} 

热点排行