FFLUSH - flush an output stream.

(ANSI Standard)

Usage:

#include <stdio.h>
ret = fflush( f );

Where:

FILE *f;
points to the file to be flushed. If this pointer is NULL, "fflush" flushes all open output units.
int ret;
is zero if the flush operation was successful; otherwise, it is non-zero.

Description:

"fflush" flushes buffered output for the file "f". The file remains open. This version of "fflush" works on terminals, disk files, and SYSOUT.

A "fflush" operation on a disk file involves a fair amount of overhead. Therefore you should not flush disk files explicitly unless the nature of the application makes it necessary (for example, if you need to flush log files to make sure they remain valid in the event of a system crash).

A "fflush" operation on a terminal also costs significant overhead, unless there is no output waiting to go to the terminal. You should generally avoid explicit flush operations on terminals, since it might make you lose input. For example, you should definitely NOT write code of the form

printf("Prompt: ");
fflush(stdout);
gets(s);

The reason is that input to the terminal is discarded unless there is an outstanding read request from the program. Since the flush operation takes place between the "printf" and the "gets", any input received after the prompt but before the "gets" is simply discarded. It is much safer to write

printf("Prompt: ");
gets(s);

and let the implicit output-flushing routines handle everything for you.

See Also:

expl c lib sleep

Copyright © 1996, Thinkage Ltd.