名稱空間
變體
操作

std::feof

來自 cppreference.com
< cpp‎ | io‎ | c
 
 
 
 
定義於標頭檔案 <cstdio>
int feof( std::FILE* stream );

檢查給定檔案流是否已到達末尾。

目錄

[編輯] 引數

stream - 要檢查的檔案流

[編輯] 返回值

若流已到達末尾,則返回非零值,否則返回 0

[編輯] 注意

此函式僅報告最近一次 I/O 操作所報告的流狀態,它不檢查關聯的資料來源。例如,如果最近一次 I/O 是 std::fgetc,它返回了檔案的最後一個位元組,則 std::feof 返回零。下一個 std::fgetc 會失敗並將流狀態更改為 *檔案結束*。只有此時 std::feof 才返回非零值。

在典型用法中,輸入流處理在任何錯誤發生時停止;然後使用 feofstd::ferror 來區分不同的錯誤條件。

[編輯] 示例

#include <cstdio>
#include <cstdlib>
 
int main()
{
    int is_ok = EXIT_FAILURE;
    FILE* fp = std::fopen("/tmp/test.txt", "w+");
    if (!fp)
    {
        std::perror("File opening failed");
        return is_ok;
    }
 
    int c; // Note: int, not char, required to handle EOF
    while ((c = std::fgetc(fp)) != EOF) // Standard C I/O file reading loop
        std::putchar(c);
 
    if (std::ferror(fp))
        std::puts("I/O error when reading");
    else if (std::feof(fp))
    {
        std::puts("End of file reached successfully");
        is_ok = EXIT_SUCCESS;
    }
 
    std::fclose(fp);
    return is_ok;
}

輸出

End of file reached successfully

[編輯] 參閱

檢查是否已到達檔案末尾
(std::basic_ios<CharT,Traits> 的公開成員函式) [編輯]
清除錯誤
(函式) [編輯]
將當前錯誤的字串顯示到 stderr
(函式) [編輯]
檢查檔案錯誤
(函式) [編輯]
C 文件 關於 feof