std::fclose(3)
π NAME π
okay so bestie, welcome to the std::fclose(3) page from the C++ Standard Libary β and the NAME section is literally:
std::fclose β std::fclose
yes, thatβs the whole thing. short, iconic, main character energy. π
β¨ Synopsis β¨
Itβs defined in header <cstdio>, and the signature is:
Defined in header <cstdio>
int fclose( std::FILE* stream );
So the way this works: it closes the given file stream. Any unwritten buffered data are flushed to the OS, and any unread buffered data are discarded β like, bye to the unread stuff, fr. π
Whether or not the operation succeeds, the stream is no longer associated with a file, and the buffer allocated by std::setbuf or std::setvbuf, if any, is also disassociated and deallocated if automatic allocation was used. no cap, that happens either way. π
β οΈ The behavior is undefined if the value of the pointerstreamis used afterfclosereturns. translation: after close, do not touchstreamβ thatβs undefined behavior, and undefined behavior is not cute. π
πΈ Parameters πΈ
streamβ the file stream to close. thatβs the whole parameter list, bestie. π«Ά
π Return value π
0on success,EOFotherwise. slay. β¨
π¦ Example π¦
The page says // Run this code, so bestie, run it β full example, preserved exactly:
#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;
}
Output, when it runs:
End of file reached successfully
π See also π
fopenβ opens a file (function). π₯freopenβ open an existing stream with a different name (function). πcloseβ flushes the put area buffer and closes the associated file (public member function ofstd::basic_filebuf<CharT,Traits>). π- C documentation for
fclose. π
xoxo, your Unix bestie π
β this page comes from the C++ Standard Libary, source/version: http://cppreference.com, date: 2024.06.10, and the manual page is std::fclose(3). bye bestie, close those streams responsibly! ππ¦
π¦ 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