std::feof(3)
π 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 ofstd::basic_ios<CharT,Traits>) π¦ the C++ streams cousin of this whole situationclearerrβ clears errors (function) π§Ή the little reset buttonperrorβ displays a character string corresponding of the current error tostderr(function) π’ the one who announces what went wrongferrorβ checks for a file error (function) π the other half of thefeof/ferrorduo, 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms