Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is fflush() as good as fclose() at saving my data to disk if the machine crashes immediately afterward?

Tags:

c++

c

We have a logger in our library that writes lines of text to disk for later diagnosis. We use it for many purposes including analyzing hard machine hangs caused by the application or our library.

We would write a line to the log file then call fclose(), then reopen it for append when we need to write another line. This is 35X slower than calling fflush() after writing a line.

So my question is, am I more likely to have the last line successfully stored in the file with the fclose() approach than the fflush() approach? If so, why? If not, what is that 35X larger amount of time busy doing if not writing the data more safely to disk?

We care the most about Windows, by the way.

like image 250
All The Rage Avatar asked Sep 25 '15 04:09

All The Rage


2 Answers

From http://en.cppreference.com/w/c/io/fflush:

For output streams (and for update streams on which the last operation was output), writes any unwritten data from the stream's buffer to the associated output device.

I would say fflush should work for your needs.

like image 53
R Sahu Avatar answered Oct 09 '22 08:10

R Sahu


Opening and closing a file for each write operation is not a optimal solution. File opens are costly operation. Also, fclose() would not guarantee that file data is flushed to the disk immediately on closing the file. The OS/filesystem will flush the data on their own logic.

fflush() will flush any data written to a file and is not yet synced to the disk. However, note that is will try to flush all blocks of file, not just latest blocks written. So if its big file and multiple applications writing to it, all of their data is flushed. However, doing flush for every write can be inefficient.

So if you really want your data to be written immediately use direct IOs, which will write data directly to the disk. And if you want to make this efficient, make them asynchronous.

like image 28
Rohan Avatar answered Oct 09 '22 06:10

Rohan