如果有一种方法在C++重置EOF状态我想知道的文件状态的结束?重置在C ifstream的对象++
回答
对于一个文件,你可以寻求到任何位置。例如,要绕回到开头:
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!
如果您已经阅读过去的结束,你必须为@Jerry棺材建议将错误标志与clear()
复位。
我试过了,只有*'seekg'之前调用'clear'时它才有效。另见这里:http://cboard.cprogramming.com/cplusplus-programming/134024-so-how-do-i-get-ifstream-start-top-file-again.html – Frank
@Frank:谢谢,编辑。我想你根本无法在一个失败的流上操作,这是有道理的。 –
对于较晚的读者:根据[cpp参考](http://en.cppreference.com/w/cpp/io/basic_istream/seekg),自C++ 11以来不再需要清除... – Aconcagua
想必你的意思上的iostream。在这种情况下,流clear()
应该完成这项工作。
我的答案同意之上,但遇到了同样的问题在今晚。所以我想我会发布一些代码,这是一个更多的教程,并显示流程的每一步的流位置。我可能应该在这里检查......在......之前,我花了一个小时来自己解决这个问题。
ifstream ifs("alpha.dat"); //open a file
if(!ifs) throw runtime_error("unable to open table file");
while(getline(ifs, line)){
//......///
}
//reset the stream for another pass
int pos = ifs.tellg();
cout<<"pos is: "<<pos<<endl; //pos is: -1 tellg() failed because the stream failed
ifs.clear();
pos = ifs.tellg();
cout<<"pos is: "<<pos<<endl; //pos is: 7742'ish (aka the end of the file)
ifs.seekg(0);
pos = ifs.tellg();
cout<<"pos is: "<<pos<<endl; //pos is: 0 and ready for action
//stream is ready for another pass
while(getline(ifs, line) { //...// }
- 1. 在C++中设置ifstream超时?
- 2. 重置的对象
- 3. C++从ifstream的
- 4. C++ - 从ifstream的
- 5. 在java中重置对象
- 6. 在C++中使用ifstream的
- 7. C++ Ifstream对象等于nullptr,但它不是一个指针?
- 8. 如何用ifstream读入对象成员?
- 9. C++ ifstream to char *
- 10. ifstream - > ofstream C++
- 11. C++ ifstream I/O?
- 12. 在pygame中重置对象位置
- 13. C#配置对象
- 14. 重置对象与构建新对象
- 15. 如何在用户定义的类中使用ifstream对象?
- 16. 正确使用ifstream对象作为类的成员
- 17. C++ ifstream类错误
- 18. C++ ifstream字符串
- 19. C++为什么结构对象值重置回零在循环
- 20. JavaScript日期对象重置
- 21. 将对象重置为零?
- 22. 重置对象键值
- 23. C++ ifstream的IS_OPEN()失败
- 24. C++ ifstream的只读整数
- 25. 重用对象在C++头定义
- 26. 在C++中改善ifstream的性能
- 27. ifstream的在C++中不接受可变
- 28. Objective-C对象放置iPad
- 29. 对象未设置C#
- 30. 如何重写配置对象设置
ifstream文件。 –