std::feof(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’œ std::feof(3) β€” C++ Standard Library, bestie edition

okay so this is the manual page for std::feof, straight out of the C++ Standard Library, and honestly? she's a little misunderstood. let's get into it 🫢


✨ NAME ✨

std::feof β€” that's it, that's the name, that's literally the whole thing, std::feof πŸ’… (yes the NAME section is just the function name twice, we love a minimal queen)


🌸 Synopsis 🌸

first things first, where does she live? Defined in header <cstdio> β€” go say hi to <cstdio> before you use her or she won't work, no cap.

int feof( std::FILE* stream );

and what does she DO? she Checks if the end of the given file stream has been reached. that's the whole vibe check. is the file done? she'll tell you. ✨


πŸŽ€ Parameters πŸŽ€

  • stream β€” the file stream to check πŸ’– this is the file you're being nosy about, she's gonna peek at it.

πŸ¦‹ Return value πŸ¦‹

so like, two possible answers:

  • Nonzero value if the end of the stream has been reached πŸ”₯
  • otherwise 0 πŸ’€ (not at the end yet, keep reading bestie)

that's it. nonzero or zero. binary queen. periodt.


🌈 Notes 🌈

okay this is the part where you need to PAY ATTENTION because this is where everyone gets confused, and honestly? valid. 😭

This function only reports the stream state as reported by the most recent I/O operation, it does not examine the associated data source. πŸ‘ˆ read that again. she is NOT looking at your actual file. she's looking at the state left behind by the last I/O operation. those are two different things, iykyk.

For example, if the most recent I/O was a std::fgetc, which returned the last byte of a file, std::feof returns zero. 🀯 likeβ€” you literally just read the final byte of the file, you'd think "okay we're done!" but nope! std::feof says zero, because nothing has failed yet.

The next std::fgetc fails and changes the stream state to end-of-file. Only then std::feof returns non-zero. πŸ’€ so she's always one step behind, she reports the past, not the future. she's not psychic, she's a historian bestie.

ngl this trips up literally everyone at least once. do not come for her, she's doing exactly what she said she'd do.

In typical usage, input stream processing stops on any error; feof and std::ferror are then used to distinguish between different error conditions. 🫢 so like: something went wrong β†’ was it "we hit the end of the file" or "something actually broke"? those two get to be told apart by feof and std::ferror. that's their whole job, they're a duo. πŸ‘‘


⭐ Example ⭐

// Run this code β€” okay here's the demo, study her:

#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;
}

πŸ‘€ quick shoutout to that comment: int c; // Note: int, not char, required to handle EOF β€” that's not me being extra, that's in the source, do NOT make it a char, it will break, I'm begging you 😭

πŸ’… Output:

End of file reached successfully

slay. ate. exactly what we wanted. πŸŽ€


πŸ”₯ See also πŸ”₯

  • eof β€” checks if end-of-file has been reached (public member function of std::basic_ios<CharT,Traits>) πŸ¦‹ the C++ streams cousin of this whole situation
  • clearerr β€” clears errors (function) 🧹 the little reset button
  • perror β€” displays a character string corresponding of the current error to stderr (function) πŸ“’ the one who announces what went wrong
  • ferror β€” checks for a file error (function) πŸ’€ the other half of the feof/ferror duo, the "was it actually broken" checker
  • C documentation for feof β€” the origin story, the C version, the blueprint πŸ’–

okay that's the whole page bestie, go forth and check your file streams responsibly 🌈


this absolute icon was brought to you by http://cppreference.com, version 2024.06.10, dated 2024.06.10 β€” mwah, sending love, see you at the next manpage πŸ’œβœ¨πŸŽ€

πŸ“¦ where this page comes from