.TH fflush() "" "" "STDIO Function (libc)"
.PC "Flush output stream's buffer"
.B "#include <stdio.h>"
\fBint fflush(\fIfp\^\fB) FILE *\fIfp\^\fB;\fR
.PP
.B fflush()
flushes any buffered output data associated with the file stream
.IR fp .
The file stream stays open after
.B fflush()
is called.
.B fclose()
calls
.BR fflush() ,
so there is no need for you to call it when normally closing a file or
buffer.
.SH Example
This example demonstrates
.BR fflush() .
When run, you will see the following:
.DM
	Line 1
	-----
	Line 1
	-----
	Line 1
	Line 2
	-----
.DE
.PP
The call
.DM
	fprintf(fp, "Line 2\en");
.DE
.PP
goes to a buffer and is not in the file when file
.B foo
is listed.
However if you redirect the output of this program to a file and list
the file, you will see:
.DM
	Line 1
	Line 1
	Line 1
	Line 2
	-----
	-----
	-----
.DE
.PP
because the line
.DM
	printf("-----\en");
.DE
.PP
goes into a buffer and is not printed
until the program is over and all buffers are flushed by
.BR exit() .
.PP
Although the \*(CO screen drivers print all output immediately, not all
operating systems work this way, so when in doubt,
.BR fflush() .
.DM
#include <stdio.h>
.DE
.DM
main()
{
	FILE *fp;
.DE
.DM
	if (NULL == (fp = fopen("foo", "w")))
		exit(1);
	fprintf (fp, "Line 1\en");
	fflush (fp);
	system ("cat foo"); /* print Line 1 */
.DE
.DM
	printf("-----\en");
	fprintf(fp, "Line 2\en");
	system("cat foo"); /* print Line 1 */
	printf("-----\en");
.DE
.DM
	fflush(fp);
	system("cat foo"); /* print Line 1 Line 2 */
	printf("-----\en");
}
.DE
.SH "See Also"
.Xr "fclose()," fclose
.Xr "libc," libc
.Xr "setbuf()," setbuf
.Xr "write()" write
.br
\*(AS, \(sc7.9.5.2
.br
\*(PX Standard, \(sc8.1
.SH Diagnostics
.B fflush()
returns
.B EOF
if it cannot flush the contents of the buffers; otherwise
it returns a meaningless value.
.PP
Note, also, that all STDIO routines are buffered.
.B fflush()
should be used to flush the output buffer if you follow
a STDIO routine with an unbuffered
routine.
