fflush -- Write Buffer to File

Format

#include <stdio.h>
int fflush(FILE *stream);

Language Level: ANSI, POSIX, XPG4
fflush causes the system to empty the buffer associated with the specified output stream, if possible. If the stream is open for input, fflush undoes the effect of any ungetc function. The stream remains open after the call.

If stream is NULL, the system flushes all open streams.

Note: The system automatically flushes buffers when you close the stream, or when a program ends normally without closing the stream.

Return Value
fflush returns the value 0 if it successfully flushes the buffer. It returns EOF if an error occurs.

Example
This example flushes a stream buffer.

#include <stdio.h>
#if (1 == __TOS_OS2__)
  #define FILENAME "myfile.htm"  /*OS/2 filename*/
#else
  #define  FILENAME "myfile.dat"  /*Windows file name*/
#endif
int main(void)
{
   FILE *stream;
   int ch;
   unsigned int result = 0;
   stream = fopen("FILENAME", "r");
   while ((ch = getc(stream)) != EOF && isdigit(ch))
      result = result * 10 + ch - '0';
   if (ch != EOF)
      ungetc(ch,stream);
   fflush(stream);   /* fflush undoes the effect of ungetc function */
   printf("The result is: %d\n", result);
   if ((ch = getc(stream)) != EOF)
      printf("The character is: %c\n", ch);
}


fclose -- Close Stream
_flushall -- Write Buffers to Files
setbuf -- Control Buffering
ungetc -- Push Character onto Input Stream
<stdio.h>