我使用了ifstream打开文件,文件名保存在一个CString中,打开不同文件时有时会出错,但是文件打开都没报错,就是数据读不出来,这是为什么?
以下是代码:
CString PathName;
PathName = L"C:\\data\\introduce.txt";
std::ifstream InFile;
InFile.open(PathName);           
int Number =0;
InFile>>Number;
 
当打开文件C:\\data\\introduce.txt时就正常,当打开C:\\data\\O.txt 时数据就写不入Number中。
运行时没有报错

解决方案 »

  1.   

    std::ifstream改为std::wifstream
    试试看
      

  2.   

    InFile.open(PathName);
    if(InFile.is_open())
    {
    .....
    }
    打开后判断一下
      

  3.   

    这个问题我碰到过,
    若要用同一个ifstream对象
    多次打开不同的文件,
    则在:
    InFile.open(PathName); 
    if(InFile.is_open()) 

        InFile.clear();//这句加上,清除以前可能出现过的错误。
    .......
      

  4.   

    InFile>> Number;这句,如果读入的不是整型,就会出错。
    判断一下:
            InFile>> Number; 
           if(!InFile.good())//判断是否正常
      cout<<"not good"<<endl;
           cout<<Number<<endl;