COHERENT manpages

This page displays the COHERENT manpage for fflush() [Flush output stream's buffer].

List of available manpages
Index


fflush() -- STDIO Function (libc)

Flush output stream's buffer
#include <stdio.h>
int fflush(fp) FILE *fp;

fflush() flushes  any buffered output data associated  with the file stream
fp. The  file stream stays  open after fflush() is  called.  fclose() calls
fflush(), so  there is no need  for you to call it  when normally closing a
file or buffer.

Example

This example demonstrates fflush(). When run, you will see the following:

    Line 1
    -----
    Line 1
    -----
    Line 1
    Line 2
    -----

The call

    fprintf(fp, "Line 2\n");

goes to a  buffer and is not in the  file when file foo is listed.  However
if you redirect the output of this program to a file and list the file, you
will see:

    Line 1
    Line 1
    Line 1
    Line 2
    -----
    -----
    -----

because the line

    printf("-----\n");

goes into  a buffer and  is not printed  until the program is  over and all
buffers are flushed by exit().

Although the COHERENT screen  drivers print all output immediately, not all
operating systems work this way, so when in doubt, fflush().

#include <stdio.h>

main()
{
    FILE *fp;

    if (NULL == (fp = fopen("foo", "w")))
        exit(1);
    fprintf (fp, "Line 1\n");
    fflush (fp);
    system ("cat foo"); /* print Line 1 */

    printf("-----\n");
    fprintf(fp, "Line 2\n");
    system("cat foo"); /* print Line 1 */
    printf("-----\n");

    fflush(fp);
    system("cat foo"); /* print Line 1 Line 2 */
    printf("-----\n");
}

See Also

fclose(),
libc,
setbuf(),
write()
ANSI Standard, §7.9.5.2
POSIX Standard, §8.1

Diagnostics

fflush()  returns EOF  if  it cannot  flush  the contents  of the  buffers;
otherwise it returns a meaningless value.

Note, also, that all STDIO  routines are buffered.  fflush() should be used
to flush the output buffer if you follow a STDIO routine with an unbuffered
routine.