std::ferror(3)

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

πŸ¦‹ std::ferror(3) πŸ¦‹ β€” C++ Standard Libary

okay so bestie, we are in the C++ Standard Libary and this is the page for std::ferror(3). The header is giving β€œtiny but iconic,” and the manual name is literally C++ Standard Libary. Yes, that spelling is what the header says, not me, ily. πŸ’…


πŸŽ€ NAME πŸŽ€

std::ferror - std::ferror

literally the name, no cap. The page is std::ferror(3), and the NAME section is just the same thing twice like a mirror selfie. πŸͺžβœ¨

πŸ“š Synopsis πŸ“š

Defined in header <cstdio>

int ferror( std::FILE* stream );

This bad boy checks the given stream for errors. You hand it a stream, it tells you if that stream has error energy. 🫢

πŸ’– Parameters πŸ’–

  • stream β€” the file stream to check. That’s the thing you’re side-eyeing for errors, bestie.

🌸 Return value 🌸

Nonzero value if the file stream has errors occurred, 0 otherwise.

So: nonzero means errors happened on that file stream; 0 means no errors found. Simple, clean, iconic. ✨

🧚 Example 🧚

Run this code, fr fr:

// Run this code

#include <clocale>
#include <cstdio>
#include <cstdlib>
#include <cwchar>

int main()
{
    const char *fname = std::tmpnam(nullptr);
    std::FILE* f = std::fopen(fname, "wb");
    std::fputs("\xff\xff\n", f); // not a valid UTF-8 character
sequence
    std::fclose(f);

    std::setlocale(LC_ALL, "en_US.utf8");
    f = std::fopen(fname, "rb");
    std::wint_t ch;
    while ((ch=std::fgetwc(f)) != WEOF) // attempt to read as UTF-8
        std::printf("%#x ", ch);

    if (std::feof(f))
        puts("EOF indicator set");
    if (std::ferror(f))
        puts("Error indicator set");
}

the stray sequence line is in the original snippet too, bestie β€” don’t come for me πŸ˜­πŸ’€

🌈 Output 🌈

Error indicator set

πŸ‘‘ See also πŸ‘‘

  • clearerr β€” clears errors. It’s a function.
  • feof β€” checks for the end-of-file. It’s a function.
  • fail β€” checks if an error has occurred. It’s a public member function of std::basic_ios<CharT,Traits>.
  • C documentation for ferror β€” yes, the C version of this whole situation.

okay that’s the whole manual page, mwah πŸ’‹ β€” source: cppreference.com, version/date: 2024.06.10, page: std::ferror(3). πŸ«ΆπŸŽ€

πŸ“¦ where this page comes from