如果有一種方法在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. 重置對象
- 5. C++從ifstream的
- 6. C++ - 從ifstream的
- 7. C++ Ifstream到一個類對象的數組
- 8. 在java中重置對象
- 9. 在C++中使用ifstream的
- 10. 重置Iterable對象
- 11. ifstream C++的問題
- 12. C++ Ifstream對象等於nullptr,但它不是一個指針?
- 13. ifstream總是將「ELF」返回到對象
- 14. 如何用ifstream讀入對象成員?
- 15. ifstream - > ofstream C++
- 16. C++ ifstream object「reseting」
- 17. C++ ifstream to char *
- 18. C++ ifstream I/O?
- 19. 在pygame中重置對象位置
- 20. C#配置對象
- 21. 重置對象與構建新對象
- 22. 團結:在C中隨機位置重疊隨機對象隨機對象#
- 23. ifstream的wchar_t的MinGW的C++
- 24. 正確使用ifstream對象作爲類的成員
- 25. 如何在用戶定義的類中使用ifstream對象?
- 26. C++ ifstream類錯誤
- 27. C++ ifstream字符串
- 28. C++ ifstream :: read()和C數組
- 29. C++爲什麼結構對象值重置回零在循環
- 30. JavaScript日期對象重置
ifstream文件。 –