| I/O manipulators | ||||
| Print functions(C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
| Synchronized Output | ||||
(C++20) | ||||
| Types | ||||
| Error category interface | ||||
(C++11) | ||||
(C++11) |
| Public member functions | ||||
(C++11) | ||||
(C++11) | ||||
(C++26) | ||||
basic_filebuf::close | ||||
| Protected member functions | ||||
| Non-member functions | ||||
(C++11) |
std::basic_filebuf<CharT, Traits>* close(); | ||
If a put area exist (e.g. file was opened for writing), first callsoverflow(Traits::eof()) to write all pending output to the file, including any unshift sequences.
If the most recently called function, out ofunderflow(),overflow(),seekpos(), andseekoff(), wasoverflow(), then callsstd::codecvt::unshift(), perhaps multiple times, to determine the unshift sequence according to the imbued locale, and writes that sequence to file withoverflow(Traits::eof()).
Then, closes the file as if by callingstd::fclose(), regardless of whether any of the preceding calls succeeded or failed.
If any of the function calls made, including the call tostd::fclose(), fails, 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, returns a null pointer right away.
In any case, updates the private member variable that is accessed byis_open().
Contents |
(none)
this on success, a null pointer on failure.
close() is typically called through the destructor ofstd::basic_filebuf (which, in turn, is typically called by the destructor ofstd::basic_fstream.
| This section is incomplete Reason: no example |
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 443 | C++98 | the file was written usingoverflow(EOF) | changed tooverflow(Traits::eof()) |
| LWG 622 | C++98 | it was unclear how to handle the exception thrown during closing | it is rethrown after closing the file |
| checks if the associated file is open (public member function)[edit] | |
[virtual] | destructs abasic_filebuf object and closes the file if it is open(virtual public member function)[edit] |