feof
From cppreference.com
| ヘッダー <stdio.h> で定義 |
||
| int feof( FILE *stream ); |
||
指定されたファイルストリームの終端に達したかどうかを確認します。
目次 |
[編集] パラメータ
| stream | - | 確認するファイルストリーム |
[編集] 戻り値
ストリームの終端に達した場合はゼロ以外の値、そうでなければ 0
[編集] 注意
この関数は、直近のI/O操作によって報告されたストリームの状態のみを報告し、関連するデータソースを検査しません。たとえば、直近のI/Oがファイルの最後のバイトを返した fgetc であった場合、feof は 0 を返します。次の fgetc は失敗し、ストリームの状態を *end-of-file* に変更します。その後に初めて feof はゼロ以外の値を返します。
典型的な使用法では、入力ストリーム処理は任意の誤りで停止します。feof と ferror は、異なるエラー条件を区別するために使用されます。
[編集] 例
このコードを実行
#include <stdio.h> #include <stdlib.h> int main(void) { const char* fname = "/tmp/unique_name.txt"; // or tmpnam(NULL); int is_ok = EXIT_FAILURE; FILE* fp = fopen(fname, "w+"); if (!fp) { perror("File opening failed"); return is_ok; } fputs("Hello, world!\n", fp); rewind(fp); int c; // note: int, not char, required to handle EOF while ((c = fgetc(fp)) != EOF) // standard C I/O file reading loop putchar(c); if (ferror(fp)) puts("I/O error when reading"); else if (feof(fp)) { puts("End of file is reached successfully"); is_ok = EXIT_SUCCESS; } fclose(fp); remove(fname); return is_ok; }
実行結果の例
Hello, world! End of file is reached successfully
[編集] 参考文献
- C11標準 (ISO/IEC 9899:2011)
- 7.21.10.2 The feof function (p: 339)