65,211
社区成员
发帖
与我相关
我的任务
分享
ifstream &openfile(ifstream & ifs,const string & str)
{
ifs.close();
ifs.clear();
ifs.open(str.c_str());
return ifs;
}
int _tmain(int argc, _TCHAR* argv[])
{
ifstream ifs;
string str,str1;
cin>>str;
while(openfile(ifs,str)>>str1,!openfile(ifs,str).eof()) //注意此处
cout<<str1;
ifs.close();
}
ifstream &openfile(ifstream & ifs,const string & str)
{
ifs.close();
ifs.clear();
ifs.open(str.c_str());
return ifs;
}
int _tmain(int argc, _TCHAR* argv[])
{
ifstream ifs;
string str,str1;
cin>>str;
ifstream& i=openfile(ifs,str);//(问题1)这里如果i不是引用会出错 为什么呢
while(i>>str1,!i.eof()) //(问题2)为什么改成这样 输出就会无限循环了呢
cout<<str1;
ifs.close();
}
ifstream &openfile(ifstream & ifs,const string & str)
{
ifs.close();
ifs.clear();
ifs.open(str.c_str());
return ifs;
}
int _tmain(int argc, _TCHAR* argv[])
{
ifstream ifs;
string str,str1;
cin>>str;
ifstream& i=openfile(ifs,str);//(问题1)这里如果i不是引用会出错 为什么呢
while(i>>str1,!i.eof()) //(问题2)为什么改成这样 输出就会无限循环了呢
cout<<str1;
ifs.close();
}
一句代码就可以搞定的,何须如此麻烦:
ifstream infile("FILE");
string str((istream_iterator<char>(infile)),istream_iterator<char>());
cout<<str;
while(i>>str1,!i.eof()) //(问题2)为什么改成这样 输出就会无限循环了呢