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 stream

&pagelevel(4)&pagelevel

Syntax

#include <stdio.h>

int fflush(FILE *stream); 

Description

If stream points to an output stream or an update stream in which the most recent operation was not input, fflush() causes any buffered data for that stream to be written to the file.
If stream is a null pointer, the flushing action is performed on all open files. 

Return val.

0

EOF

if successful. The buffer was flushed.

if an error occurs. The buffer was not flushed. errno is set to indicate the error.

BS2000
Alternatively, the buffer did not need to be flushed, since it does not exist (because no write function has been executed on the file), or the file is an input or INCORE file. (End)

stream is not associated with any file (since the file is already closed, for example) or the buffered data could not be transferred.

Errors

fflush() will fail if: 

 

EAGAIN

The O_NONBLOCK flag is set for the file descriptor underlying stream, and the process would be delayed in the write operation.

 

EBADF

The file descriptor underlying stream is not valid.

 

EFBIG

An attempt was made to write a file that exceeds the maximum file size or the process file size limit (see also ulimit()). 

 

EINTR

fflush() was interrupted by a signal.

 

EIO

An I/O error occurred.

The process is a member of a background process group attempting to write to its controlling terminal; TOSTOP is set; the process is neither ignoring nor blocking SIGTTOU, and the process group of the process is orphaned.

 

ENOSPC

There was no free space remaining on the device containing the file.

 

EPIPE

An attempt is made to write to a pipe or FIFO that is not open for reading by any process. A SIGPIPE signal will also be sent to the process.

If threads are used, then the function affects the process or a thread in the following manner: If an EPIPE error occurs, the SIGPIPE signal is not sent to the process, but is sent to the calling thread instead.

Notes

The program environment determines whether fflush() is executed for a BS2000 or POSIX file.

BS2000
All standard I/O functions that write data to a BS2000 file (printf(), putc(), fwrite(), etc.) store this data temporarily in a buffer and only write it to the file when one of the following events occurs:

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

  • The maximum record length of a disk file is reached.

  • For terminals: when output to the terminal is followed by input from the terminal.

  • The functions fseek(), fsetpos(), rewind() or fflush() 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 buffer, the data of all ISAM files still stored in buffers is automatically written out to the files.

Buffering does not take place in the case of outputs to strings (sprintf()) and to INCORE 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 extends the record in the file. Consequently, when an ISAM file is read, only those newline characters explicitly written by the program are read in.

fflush() is automatically executed internally when a file is closed (fclose(), close()) or when a program ends normally or is terminated by means of an 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.
In the case of record I/O, calls to the fflush() function are not rejected with an error, but have no effect. No data is buffered for files with record I/O. (End) 

See also

exit(), close(), fclose(), stdio.h.