首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

ifstream中getline读入过长数据后处于fail状态如何恢复

2012-10-05 
ifstream中getline读入过长数据后处于fail状态怎么恢复char buff[200]fstream tmpfiletmpfile.open(C:\

ifstream中getline读入过长数据后处于fail状态怎么恢复
char buff[200];
fstream tmpfile;

tmpfile.open("C:\\test.txt", ios::in);

while( !tmpfile.eof() )
{
  tmpfile.getline(buff, sizeof(buff));
  cout <<buff;
}

C:\test.txt是一篇文章,其中有一段文字超过200byte,getline可以将200byte以前的内容读入buff中,但在这之后io流的fail标志位变为1,其余标志位全部变成0,io流不响应读取操作,循环也无法结束。

当遇到这种状况时,我想让其恢复good状态并继续从200byte之后读取数据,请问可以吗?应该怎么做?

[解决办法]
flush 或者 clear
[解决办法]
好像不行,我推荐你一个好的办法,用string 类:

C/C++ code
#include <iostream>#include <fstream>#include <string>using namespace std;int main(){    string strBuff;    fstream tmpfile;        tmpfile.open("C:\\test.txt", ios::in);        while( !tmpfile.eof() )    {        getline(tmpfile, strBuff);        cout <<strBuff<<endl;    }        return 0;}
[解决办法]
tmpfile.clear()
[解决办法]
C/C++ code
string str;while(getline(tempFile,str)){} 

热点排行