首页 > 编程知识 正文

ifstream读二进制文件,ifstream用法

时间:2023-05-05 19:41:53 阅读:57466 作者:4689

文件输入流(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; }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。