文件输入流(ifstream )读取文件末尾后,调用seekg重定向pos
类似于下面的代码片段。
//readwholefilewhile (ifs.readline (str,strLen ) ) STD 33603360 cout line ' : ' str STD 33603360 endl; }ifs.seekg(0,std:ios:beg ); //rewindtobeginningofthefile//1ifs.clear (; //cleareofflag//2STD :3360 cout ' ' STD :3360 endl;//读取whole file again line=0; 出现while(ifs.readline(str,strLen ) ) /错误,文件STD :3360 cout line ' : ' str STD 3360: endl; }
重新读取文件时发生错误,无法继续读取文件。
如果你看seekg的说明
如果未清除ifstream的eofbit,seekg将失败。
更改为以下代码后,程序正常了。
//readwholefilewhile (ifs.readline (str,strLen ) ) STD 33603360 cout line ' : ' str STD 33603360 endl; }ifs.clear (; //cleareofflagfirst//2ifs.seekg (0,STD:3360IOs33603360Beg ); //thenrewindtobeginningofthefile//1STD :3360 cout ' ' STD 3360: endl;//读取whole file again line=0; 出现while(ifs.readline(str,strLen ) ) /错误,文件STD :3360 cout line ' : ' str STD 3360: endl; }