std::basic_ios<CharT,Traits>::eof
提供: cppreference.com
bool eof() const; |
||
紐付けられているストリームがファイル終端に達していれば true を返します。 具体的には、 rdstate() の eofbit
がセットされていれば true を返します。
eofbit
がセットされる状況の一覧は ios_base::iostate を参照してください。
目次 |
[編集] 引数
(なし)
[編集] 戻り値
ファイル終端が発生した場合は true、そうでなければ false。
[編集] ノート
この関数は最も最近の入出力操作によってセットされたストリームの状態を報告するだけです。 紐付けられているデータソースは調べません。 例えば、最も最近の入出力がファイルの最後のバイトを返した get() であった場合、 eof()
は false を返します。 次の get()
は読み込みに失敗し、 eofbit
をセットします。 その後にのみ eof()
は true を返します。
一般的な使用方法では、入力ストリームの処理は何らかのエラーが発生したときに停止します。 eof()
と fail() は異なるエラー状況を区別するために使用することができます。
[編集] 例
Run this code
#include <iostream> #include <fstream> #include <cstdlib> int main() { std::ifstream file("test.txt"); if(!file) // operator! is used here { std::cout << "File opening failed\n"; return EXIT_FAILURE; } // typical C++ I/O loop uses the return value of the I/O function // as the loop controlling condition, operator bool() is used here for(int n; file >> n; ) { std::cout << n << ' '; } std::cout << '\n'; if (file.bad()) std::cout << "I/O error while reading\n"; else if (file.eof()) std::cout << "End of file reached successfully\n"; else if (file.fail()) std::cout << "Non-integer data encountered\n"; }
[編集] 関連項目
以下の表は ios_base::iostate フラグのすべての有り得る組み合わせに対する basic_ios
のアクセサ (good()、 fail() など) の値を示します。
ios_base::iostate のフラグ | basic_ios のアクセサ
| |||||||
eofbit | failbit | badbit | good() | fail() | bad() | eof() | operator bool | operator! |
false | false | false | true | false | false | false | true | false |
false | false | true | false | true | true | false | false | true |
false | true | false | false | true | false | false | false | true |
false | true | true | false | true | true | false | false | true |
true | false | false | false | false | false | true | true | false |
true | false | true | false | true | true | true | false | true |
true | true | false | false | true | false | true | false | true |
true | true | true | false | true | true | true | false | true |
ファイルの終端かどうか調べます (関数) |