命名空间
变体
操作

std::feof

来自 cppreference.cn
< 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