std::basic_ios<CharT,Traits>::eof
From cppreference.com
| bool eof() const; |
||
関連ストリームがファイルの終端に達した場合は true を返します。具体的には、rdstate() で `eofbit` が設定されている場合に true を返します。
eofbit を設定する条件のリストについては、ios_base::iostate を参照してください。
目次 |
[編集] パラメータ
(なし)
[編集] 戻り値
ファイルの終端が発生した場合は true、それ以外の場合は false。
[編集] 注釈
この関数は、直近の I/O 操作によって設定されたストリームの状態のみを報告します。関連するデータソースを調べるわけではありません。たとえば、直近の I/O がファイルの最後のバイトを返した get() であった場合、eof() は false を返します。次の `get()` は何も読み取れずに失敗し、eofbit を設定します。その後に初めて eof() は true を返します。
通常の利用では、入力ストリーム処理はどのようなエラーでも停止します。その後、eof() と fail() を使用して、異なるエラー条件を区別できます。
[編集] 例
このコードを実行
#include <cstdlib> #include <fstream> #include <iostream> 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 |
| ファイルの終端をチェックする (関数) |