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).

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

 Vote 0

Similar topics related to this section

write vs append mode, binary vs text mode, binary mode, fseek, ftell, rewind, fprintf, fscanf, fflush, poll, select, fcntl, fputs,fgets,fgetc,fputc, printf, scanf,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan