int _tmain (int argc, _TCHAR* argv[])
{
string filename="hello.txt";
ifstream input;
input.open (filename.c_str ());
if (input.is_open ())
{
cout<<"opened"<<endl;
}
if (!input )
if (input.bad ())
cout<<"bad"<<endl;
if (input.fail ())
cout<<"fail"<<endl;
return -1;
f (input );
system ("pause");
return 0;
}
istream &f (istream &in )
{
string temp;
while (in>>temp,!in.eof ())
{
if (in.bad ())
throw runtime_error ("IO stream corrupted");
if (in.fail ())
{
cerr<<"bad data,try again!";
in.clear ();
in.setstate (istream::eofbit );
continue;
}
cout<<temp<<endl;
}
in.clear ();
return in;
}
代码如上,总是跳到文件打开失败语句, hello.txt 文件我放在与 exe 文件同一个目录下。
百思不得其解啊?为什么呢?
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.