What does std :: ofstream :: close () do?

This question: How to protect the log from application failure? led me to another - what does std::ofstream::close()it really do? I know what he calls flush()and this one. But what else? Really closing a file?

Edit: Let me rephrase my question - is it something physically done for the actual file during the call, close()or is it just std::ofstreaminternal cleaning stuff?

+5
source share
3 answers

In addition to flushing user space buffers, i.e. flush(), close(2)called in the base file descriptor. It depends on the operating system, what happens then, but most likely nothing happens with the actual storage occupied by the file.

What will happen is that (if the file descriptor was the last link in this process to this file), the file record associated with the file is deleted from the table of open process files. That is, the release of process-related kernel memory.

+3
source

Here is the call trace from the documentation:


void std::basic_ofstream::close();

Effectively causes rdbuf()->close(). If an error occurs during operation, it is called setstate(failbit).


std::basic_streambuf<CharT,Traits>* std::basic_ofstream::rdbuf() const;

Returns the associated buffer stream. If there is no associated stream buffer, returns NULL.


std::basic_streambuf std::basic_filebuf, :


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

(, ), overflow(Traits::eof()), , unshift.

, underflow(), overflow(), seekpos() seekoff(), overflow(), std::codecvt::unshift(), , , unshift overflow(Traits::eof()).

, std::fclose, , - .

. close() std::basic_filebuf (, , std::basic_fstream.


, , flush() , . std::basic_filebuf::close(). , , - , .. unshift. , .

: std::basic_ofstream::close().

+3

, , .

.

( , ), .

Linking the stream file is supported by its internal stream buffer: Internally, the function calls rdbuf () → close () and sets failbit in case of failure.

Note that any open file closes automatically when the thestream object is destroyed.

From: http://www.cplusplus.com/reference/fstream/ofstream/close/

+1
source

All Articles