C++问题:ifstream流一直找不到文件结束符 while循环一直没有跳出
#include<stdlib.h>#include<fstream>#include<iostream>intmain(){vector<int>files;ifstr...
#include <stdlib.h>
#include <fstream>
#include <iostream>
int main()
{
vector<int> files;
ifstream infile("messi.txt");
if(!infile)
{
cerr<<"cannot open the file:"
<<"ronaldo.txt"<<endl;
}
int tofile;
while(infile>>tofile,!infile.eof())//以行读入 getline(infile,tofile)//流
{ //状
if(infile.bad()) //态 //8.9和8.10
throw runtime_error("IO stream ocorrupt"); //的
if(infile.fail()) //查
{ //询
cerr<<"bad data,try again.."<<endl; //和
infile.clear(); // //控
//infile.ignore(numeric_limits<streamsize>::max(),'\n'); //制
continue;
}
if(!infile.fail())
{
files.push_back(tofile);
}
}
vector<int>::iterator ster=files.begin();
for(;ster!=files.end();ster++)
{
cout<<*ster<<endl;
}
infile.close();
infile.clear();
system("pause ");
return 0;
}
messi.txt文件内容如下:
data milan london 43 12 liverpool chelsea 32 enter
lionel 32 enter
12 980 enter
执行结果一直是输出bad data,try again..并且停不下来了 求高手解答。。。 展开
#include <fstream>
#include <iostream>
int main()
{
vector<int> files;
ifstream infile("messi.txt");
if(!infile)
{
cerr<<"cannot open the file:"
<<"ronaldo.txt"<<endl;
}
int tofile;
while(infile>>tofile,!infile.eof())//以行读入 getline(infile,tofile)//流
{ //状
if(infile.bad()) //态 //8.9和8.10
throw runtime_error("IO stream ocorrupt"); //的
if(infile.fail()) //查
{ //询
cerr<<"bad data,try again.."<<endl; //和
infile.clear(); // //控
//infile.ignore(numeric_limits<streamsize>::max(),'\n'); //制
continue;
}
if(!infile.fail())
{
files.push_back(tofile);
}
}
vector<int>::iterator ster=files.begin();
for(;ster!=files.end();ster++)
{
cout<<*ster<<endl;
}
infile.close();
infile.clear();
system("pause ");
return 0;
}
messi.txt文件内容如下:
data milan london 43 12 liverpool chelsea 32 enter
lionel 32 enter
12 980 enter
执行结果一直是输出bad data,try again..并且停不下来了 求高手解答。。。 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询