File Buffer Flushing

C file APIs are wrapper over non-buffered system calls. This wrapper function like fread()/fwrate() or fprintf()/fscanf() works on internally allocated buffer s so to avoid multiple disk read/writes or multiple console I/Os. Lets understand this in a little bit details. We often call fprintf() with a size of few bytes. Now files are stored as disk sectors which have a minimum size of 512 bytes. Now if fprintf() writes every time when we call fprintf() then it would access disk each time and it would be slow. To optimize these operations, C runtime uses an internal buffer which has a size of multiple of 512 and it stores all data to this buffer when fprintf() are called. Now user may be writing lots of fprintf() so this buffer will be filled at one point of time. Then C runtime will flush the buffer internally to current sector and again use this buffer for next fprintf() calls and same way it will be flushed for the next file sector. Another sitiation may happen when user write few bytes with fprintf and want to close this file. Then also this buffer will be flushed to current sector and file will be closed. Here we are observing that C runtime takes care of flushing buffer but what if users want to flush buffer explicitly? C runtime provides fflush() for this purpose.


The C library function int fflush(FILE *stream) flushes the output buffer of a stream.


Following is the declaration for fflush() function.
int fflush(FILE *stream)


  • stream -- This is the pointer to a FILE object that specifies a buffered stream.

Return Value

This function returns zero value on success. If an error occurs, EOF is returned and the error indicator is set(i.e. feof).

About our authors: Team EQA

You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.