Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I wonders when I "write" a file in c/c++, How and when does it write to disk ?
Thanks for your attention.
Posted

1 solution

When you write to the file, data is not always physically written to the disk, as the disk operation is buffered (cached), for the sake of performance. Flushing guarantees that already written part of data is committed to the disk. If your process gets terminated unexpectedly, the already written part of file will be preserved. When you close the file, the data is flushed before closing.

Maybe this explanation is overly simplified, but I think, for writing application, it should be enough to take into account in practical consideration. For more detail, please see:
http://en.wikipedia.org/wiki/Cache_%28computing%29[^],
http://en.wikipedia.org/wiki/Disk_buffer[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900