Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

fflush - Flush file buffers

&pagelevel(4)&pagelevel

Definition  

#include <stdio.h>

int fflush(FILE *fp);

fflush clears the buffer for the file pointed to by file pointer fp and writes the data that was temporarily stored in the buffer to this file. If fp is a NULL pointer, fflush performs these activities for all open files.

Return val.

0

fflush has flushed the buffer, or no buffer needs to be flushed because:

  • the buffer does not yet exist (a write function has not yet been executed for the file) or

  • the file is an input or INCORE file.

EOF

fflush has not flushed the buffer because:

  • the pointer fp is not assigned to a file (e.g. because the file is already closed) or

  • the buffered data could not be transferred.

Notes

All standard I/O functions that write data to a file (printf, putc, fwrite etc.) store this data temporarily in an internal C buffer and only write it to the file when one of the following events occurs (See also section “Buffering” (Basic terms). Buffering does not take place in the case of outputs to strings (sprintf) and to INCORE files.):

  • a newline character (\n) is detected (only for text files)

  • the maximum record length of a disk file is reached

  • for data display terminals: output to the terminal is followed by input from the terminal

  • the fseek/fseek64, fsetpos/fsetpos64, rewind or fflush functions are called

  • the file is closed.

  • In addition, for ANSI functionality only:
    If reading from any text file makes data transfer necessary from the external file to the internal C buffer, the data of all ISAM files still stored in buffers is automatically written out to the files.

fflush causes a line change in a text file even if the data in the buffer does not end with a newline character. Data that follows is written to a new line (or a new record).

Exception for ANSI functionality:
If the data of an ISAM file in the buffer does not end in a newline character, fflush does not cause a change of line (or change of record). Subsequent data lengthens the record in the file. When an ISAM file is read, therefore, only those newline characters explicitly written by the program are read in.

Internally, fflush is executed automatically when a file is closed (fclose, close) or when a program ends normally or is terminated by means of exit.

fflush can be used to control the output of data during program execution, e.g. to concatenate various inputs into a single output and print them together at a user-defined point in time (cf. example).

Record I/O

A call to the fflush function is not rejected with an error, but it has no effect. No data is buffered in the case of files with record I/O.

Example

The following program reads alphabetically sorted names from stdin and outputs them to a file. Names that begin with the same letter are to be written in the same record of the file, separated from each other by a space. For “ANSI” functionality, the desired result is achieved only if output is to a SAM file. When output is to ISAM files, all names are written to one record, since fflush does not cause a change of record.

#include<stdio.h>
int main(void)
{
  FILE *fp;
  char name[20];
  char prevname;
  prevname = '%';
  fp = fopen ("link=link", "w");
  while (gets(name))
  {
    if(prevname != name[0])
       fflush(fp);
    else
       fputc(' ', fp);
    fputs(name, fp);
    prevname = name[0];
  };
  fclose(fp);
  return 0;
}

See also

exit, close, fclose