一个简单的 C++ 程序,我到底错哪了??
#include <iostream> #include <string>
#include <fstream>
using namespace std;
istream& f( istream &in )
{
string ival;
while (in >> ival, !in.eof())
{
if(in.bad())
throw runtime_error("IO streamcorrupted");
if ( in.fail() ) // bad input
{
cerr << " bad date, try again:";
in.clear( );
continue;
}
cout << ival << endl;
}
in.clear();
return in;
}
int main()
{
string filename;
cout << " enter the filename ( ctrl + z to end ):\n";
cin>>filename;
ifstream infile(filename.c_str());
if(!infile)
{
cerr << " error: cannot open the input file:" << filename << endl;
return -1;
}
f( infile );
system("pause");
return 0;
}
我那txt文件里面写的是hello word 然后我运行程序输入路径E:\\hello.txt 却什么也没有,文件也成功打开了(请按任意键继续。。。。)
求大神求解!!!