LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1113|回复: 2

能不能问个初级读取文件跳过错误数据问题?谢谢![已解决]

[复制链接]
发表于 2005-11-29 11:26:22 | 显示全部楼层 |阅读模式
能不能问个初级读取文件问题?谢谢!
如何在读取文件时跳过错误数据?大致这样:

  1. ifstream ifile("1.dat");//一个数据文件,大致为
  2. 62
  3. 85
  4. /
  5. 60
  6. 99

  7. //我这样读文件。
  8. float data;
  9. for(;;)
  10. {
  11. ifile>>data;
  12. cout<<data<<endl;
  13. if (ifile.eof())
  14. {
  15.         cout<<"The whole file has been read."<<endl;
  16.         break;
  17. }
  18. else if ((ifile.bad())||(ifile.fail()))
  19. {
  20.         cout<<"There is bad data in the input file."<<endl;
  21.         cout<<"The program will ignore the bad data."<<endl;
  22.         ifile.seekg(2*sizeof(float),ios::cur);//我尝试把当前位置后移2位,错误?
  23.         ifile.clear();//清除错误标志
  24.         getchar();
  25. }
  26. }
复制代码

我用以上方法尝试跳过"/"不成功。怎样才能跳过错误数据 "/"?
运行结果是
There is bad data in the input file.
The program will ignore the bad data.
我回车后会输出 85 ,然后又是这样,死循环。何解?
大虾指点一下拉
 楼主| 发表于 2005-11-29 19:29:54 | 显示全部楼层
Post by hhccnu
能不能问个初级读取文件问题?谢谢!
如何在读取文件时跳过错误数据?大致这样:

  1. ifstream ifile("1.dat");//一个数据文件,大致为
  2. 62
  3. 85
  4. /
  5. 60
  6. 99

  7. //我这样读文件。
  8. float data;
  9. for(;;)
  10. {
  11. ifile>>data;
  12. cout<<data<<endl;
  13. if (ifile.eof())
  14. {
  15.         cout<<"The whole file has been read."<<endl;
  16.         break;
  17. }
  18. else if ((ifile.bad())||(ifile.fail()))
  19. {
  20.         cout<<"There is bad data in the input file."<<endl;
  21.         cout<<"The program will ignore the bad data."<<endl;
  22.         ifile.seekg(2*sizeof(float),ios::cur);//我尝试把当前位置后移2位,错误?
  23.         ifile.clear();//清除错误标志
  24.         getchar();
  25. }
  26. }
复制代码

我用以上方法尝试跳过"/"不成功。怎样才能跳过错误数据 "/"?
运行结果是
There is bad data in the input file.
The program will ignore the bad data.
我回车后会输出 85 ,然后又是这样,死循环。何解?
大虾指点一下拉

搞好了,我用字符串读取,判断为数值后,然后atof()转换。

麻烦请斑竹删贴把。
回复 支持 反对

使用道具 举报

发表于 2005-11-30 23:53:51 | 显示全部楼层
过程比结果更重要,所以,帖就删了 ^_^
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表