<reponame>rpuntaie/c-examples<filename>cpp/io_c_feof.cpp<gh_stars>0
/*
g++ --std=c++20 -pthread -o ../_build/cpp/io_c_feof.exe ./cpp/io_c_feof.cpp && (cd ../_build/cpp/;./io_c_feof.exe) || true
https://en.cppreference.com/w/cpp/io/c/feof
*/
#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;
}

