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()
でエラーフラグをリセットする必要があります。
私はこれを試しました。これは、 'clear'が* see *' seekg'の前に呼び出された場合にのみ機能します。こちらもご覧ください:http://cboard.cprogramming.com/cplusplus-programming/134024-so-how-do-i-get-ifstream-start-top-file-again.html – Frank
@フランク:ありがとう、編集済み。私はあなたが失敗したストリームでは全く動作できないと思います。 –
後半の読者の場合:[cppリファレンス](http://en.cppreference.com/w/cpp/io/basic_istream/seekg)によると、C++ 11以降は削除する必要はありません。 – Aconcagua
おそらくiostreamを意味します。この場合、ストリームのclear()
が処理を行う必要があります。
私は上記の答えに同意しますが、今夜同じ問題に遭遇しました。だから私はもう少しチュートリアルで、プロセスの各段階でストリームの位置を示すコードをいくつか投稿すると思った。私はおそらくここにチェックしておくべきだった...前に...私は1時間を自分で考え出した。
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. Ifstream - EOFビットをリセットする
- 2. C++:EOF(ファイルの終わり)でコードエラーを実行する
- 3. ファイルの終わり
- 4. ifstreamオブジェクトを通してC++でファイルを読む
- 5. ifstream.eof() - ファイルの終わりが実際の終わりに達するまで
- 6. 月の終わりまでのxtsオブジェクト
- 7. c#asp.netリダイレクトコードの終わり
- 8. ファイルの終わりまでの読み方C、fscanf
- 9. ランタイムエラー:ファイルの終わり
- 10. C Makefile - 異なるファイルの終わりのサブディレクトリ
- 11. Cプログラミング - ファイルの終わりの1の補数
- 12. ファイルの終わりのチェックが機能しません(C++)
- 13. C++:一部のデータ型でnoskipwsがファイルの終わりを遅らせる
- 14. for_eachで終わりに終わり
- 15. オブジェクトの終わりでJSONストリーム解析がクラッシュする
- 16. C++ ifstreamはファイルを開けません
- 17. Cで「ファイルの終わり」とは何を意味していますか?
- 18. 改行でファイルの終わりを検出するには?
- 19. pexpect.EOF:ファイルの終わり(EOF)。例外スタイルプラットフォーム
- 20. COBOLファイルの終わりopen-cobol
- 21. PLS-00103:「ファイルの終わり」次
- 22. C#のテキストボックスの終わりにユニットを追加する方法
- 23. 既存のFlowDocument/Paragraphオブジェクトの代わりに書式設定をリセットする
- 24. リストタイトル終わり最後の単語で終わり
- 25. COMオブジェクト - C#でCOMオブジェクトを開発する代わりに、型のオブジェクト
- 26. C++文字列ストリームの終わりを取得するには?
- 27. BufferedReader - ストリームの終わりでブロックする
- 28. TCLでファイルを終わりから終わりに(逆の順序で)読み込む方法は?
- 29. はOpenCVでファイルの終わりで区切られます
- 30. NSPredicateが複数のファイルで終わる
ifstreamファイル。 –