What is fflush exactly and what does it do?

954 Views Asked by At

I was reading http://www.cplusplus.com/reference/cstdio/fflush/ and I was curious about what it means. According to the website it says:

If the given stream was open for writing (or if it was open for updating and the last i/o operation was an output operation) any unwritten data in its output buffer is written to the file.

What does the output buffer to file mean?

2

There are 2 best solutions below

1
On BEST ANSWER

Some streams buffer output data and do not write to the device immediately. fflush forces the contents of the stream's buffer, if there is one, to be written to the device and the buffer cleared.

2
On

Generally, when data is intended to be written to a file, it is stored in a construct known as a "buffer". Once the buffer has reached a certain threshold, all the data stored in the buffer will be written out to the file at once.

Fflush empties the buffer and forces all changes to be written to the file. This is particularly useful when you are done writing to the file, since it is good practice to flush the buffer before closing the file (thereby making sure that all data has been successfully written to the file).

This goes for other types of filestreams too.