|
|

楼主 |
发表于 2005-11-29 19:29:54
|
显示全部楼层
Post by hhccnu
能不能问个初级读取文件问题?谢谢!
如何在读取文件时跳过错误数据?大致这样:
- ifstream ifile("1.dat");//一个数据文件,大致为
- 62
- 85
- /
- 60
- 99
- //我这样读文件。
- float data;
- for(;;)
- {
- ifile>>data;
- cout<<data<<endl;
- if (ifile.eof())
- {
- cout<<"The whole file has been read."<<endl;
- break;
- }
- else if ((ifile.bad())||(ifile.fail()))
- {
- cout<<"There is bad data in the input file."<<endl;
- cout<<"The program will ignore the bad data."<<endl;
- ifile.seekg(2*sizeof(float),ios::cur);//我尝试把当前位置后移2位,错误?
- ifile.clear();//清除错误标志
- getchar();
- }
- }
复制代码
我用以上方法尝试跳过"/"不成功。怎样才能跳过错误数据 "/"?
运行结果是
There is bad data in the input file.
The program will ignore the bad data.
我回车后会输出 85 ,然后又是这样,死循环。何解?
大虾指点一下拉
搞好了,我用字符串读取,判断为数值后,然后atof()转换。
麻烦请斑竹删贴把。 |
|