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 类:
#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()
[解决办法]
string str;while(getline(tempFile,str)){}