static_rtti
static_rtti

Reputation: 56282

Does closing an fstream guarantee a filesystem sync?

Does closing an fstream guarantee a sync to the filesystem? I'm debating this with a co-worker and would need a definite reference. I'm intersted in what the standard says and also in what happens on windows with the Visual Studio implementation.

Also, if close doesn't imply a sync, is there a standard C++ way to ensure the sync is done?

Upvotes: 2

Views: 1517

Answers (2)

Solomon Ucko
Solomon Ucko

Reputation: 6109

std::basic_filebuf::close calls std::fclose, which states that: "Any unwritten buffered data are flushed to the OS", or according to the C99 standard draft n1256, "Any unwritten buffered data for the stream are delivered to the host environment to be written to the file".

Upvotes: 0

quetzalcoatl
quetzalcoatl

Reputation: 33536

I'm quite sure it's already sorted out how closing the filestream closes the buffers. So, now, the buffers:

draft of Std'1998: 27.8.1.3.6: basic_filebuf* close();

6 Effects: If is_open() == false, returns a null pointer. If a put area exists, calls overflow(EOF) to flush characters. (...) Finally it closes the file (‘‘as if’’ by calling std::fclose(file)).308) If any of the calls to overflow or std::fclose fails then close fails.

Now, see the overflow(): 27.8.1.4.8 (...)

I've then tried to trace it further, somewhere down there were some references to sync and sputc, but I was unable to trace the exact wording for how overflow guarantees flushing. It surely does, but sorry, my time's up today :/

Upvotes: 1

Related Questions