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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With