在C ++中重置ifstream对象的文件结束状态 [英] Resetting the End of file state of a ifstream object in C++
本文介绍了在C ++中重置ifstream对象的文件结束状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否可以在C ++中重置eof状态?
I was wondering if there was a way to reset the eof state in C++?
推荐答案
对于文件,您可以只是寻找任何位置。例如,倒退到开头:
For a file, you can just seek to any position. For example, to rewind to the beginning:
std::ifstream infile("hello.txt");
while (infile.read(...)) { /*...*/ } // etc etc
infile.clear(); // clear fail and eof bits
infile.seekg(0, std::ios::beg); // back to the start!
如果您已读完结尾,则必须使用 clear()
,如@Jerry Coffin建议。
If you already read past the end, you have to reset the error flags with clear()
as @Jerry Coffin suggests.
这篇关于在C ++中重置ifstream对象的文件结束状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文