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

"Cannot open the file" Why?该如何处理

2012-02-25 
Cannot open the file!Why?voidmain(){stringinfileifstreamin(infile.c_str())if(!in){cout Can

"Cannot open the file"! Why?
void   main()
{
        string   infile;
        ifstream   in(infile.c_str());
        if(!in)
        {
                cout < < "Cannot   open   the   file " < <endl;
        }
        else  
                cout < < "got   it " < <endl;
}
结果输出   "Cannot   open   the   file "   为什么呢?请指教!

[解决办法]
因为你的infile变量什么也没有...
[解决办法]
string infile;
要换成
string infile= "c:\\abc.txt ";//换成你要打开的文件的名称
[解决办法]
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
string infile;
cout < < "Enter input file name: "
cin > > infile;

ifstream in(infile.c_str());
if(!in)
cout < < "Cannot open the file: " < < infile < <endl;
else
cout < < "got it " < <endl;

in.close();

return 0;
}

热点排行