Here is the trace of calls from documentation:
void std::basic_ofstream::close();
Effectively calls rdbuf()->close()
. If an error occurs during operation, setstate(failbit)
is called.
std::basic_streambuf<CharT,Traits>* std::basic_ofstream::rdbuf() const;
Returns the associated stream buffer. If there is no associated stream buffer, returns NULL
.
std::basic_streambuf
actually inherits std::basic_filebuf
, therefore:
std::basic_filebuf<CharT, Traits>* std::basic_filebuf::close();
If a put area exist (e.g. file was opened for writing), 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 calls std::codecvt::unshift()
, perhaps multiple times, to determine the unshift sequence according to the imbued locale, and writes that sequence to file with overflow(Traits::eof())
.
Then, closes the file as if by calling std::fclose
, regardless of whether any of the preceding calls succeeded or failed.
NOTE: close()
is typically called through the destructor of std::basic_filebuf
(which, in turn, is typically called by the destructor of std::basic_fstream
.
First of all, we can see that it doesn't actually call flush()
directly as you expected. Nevertheless, the flushing effect indeed occurs in the std::basic_filebuf::close()
method. In addition, we can see that it still does a bit of tampering with a file, i.e. writing the unshift sequence. Nothing else special happens then, the file simply closes.
Pay attention to the NOTE above: in most cases you don't even need to call std::basic_ofstream::close()
explicitly.