std::basic_filebuf::close(3)

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

πŸ’– NAME πŸ’–

okay bestie, welcome to the manual page for std::basic_filebuf::close(3) from the C++ Standard Libary β€” yes, that's the spelling in the header and we're keeping it, no notes 😭✨

std::basic_filebuf::close β€” literally the name, and also std::basic_filebuf::close again. It's giving "the NAME line said it twice and we respect the hustle" πŸ’…


✨ Synopsis ✨

std::basic_filebuf<CharT, Traits>* close();

So here's the tea: if a put area exists (like when the file was opened for writing), it first calls overflow(Traits::eof()) to write all pending output to the file, including any unshift sequences. 🫢

If the most recently called function out of underflow(), overflow(), seekpos(), and seekoff() was overflow(), then it calls std::codecvt::unshift(), maybe multiple times, to determine the unshift sequence according to the imbued locale, and writes that sequence to the file with overflow(Traits::eof()). ✨

Then it closes the file as if by calling std::fclose(), regardless of whether any of the preceding calls succeeded or failed. no cap.

If any of the function calls made, including the call to std::fclose(), fails, it returns a null pointer. If any of the function calls made throws an exception, the exception is caught and rethrown after closing the file. If the file is already closed, it returns a null pointer right away. πŸ’€

In any case, it updates the private member variable that is accessed by is_open(). πŸ‘‘

πŸŽ€ Parameters πŸŽ€

  • (none) β€” literally none, bestie. πŸŽ€

🌸 Return value 🌸

  • this on success
  • a null pointer on failure

πŸ¦‹ Notes πŸ¦‹

close() is typically called through the destructor of std::basic_filebuf (which, in turn, is typically called by the destructor of std::basic_fstream). so it's kind of a background main character, yk? πŸ’œ

⭐ Example ⭐

This section is incomplete Reason: no example πŸ™ˆ

🌈 Defect reports 🌈

The following behavior-changing defect reports were applied retroactively to previously published C++ standards. πŸ“š

  • DR LWG 443, applied to C++98 β€” behavior as published: the file was written using overflow(EOF); correct behavior: changed to overflow(Traits::eof()). πŸ”₯
  • DR LWG 622, applied to C++98 β€” behavior as published: it was unclear how to handle the exception thrown during closing; correct behavior: it is rethrown after closing the file. πŸ’…

πŸ‘€ See also πŸ‘€

  • is_open β€” checks if the associated file is open (public member function)
  • destructor β€” destructs a basic_filebuf object and closes the file if it is open [virtual] (virtual public member function)

🏷️ Category 🏷️

  • * Todo no example

source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::basic_filebuf::close(3) β€” bye bestie, mwah πŸ’‹πŸŽ€

πŸ“¦ where this page comes from