读书人

quot;Cannot open the filequot; Why?该如何处

发布时间: 2012-02-25 10:01:48 作者: rapoo

"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;
}

读书人网 >C++

热点推荐