NAME
setbuf, setbuffer, setlinebuf - assign buffering to a
stream
SYNOPSIS
#include <stdio.h>
void setbuf(stream, buf)
FILE *stream;
char *buf;
void setbuffer(stream, buf,
size)
FILE *stream;
char *buf;
int size;
int setlinebuf(stream)
FILE *stream;
DESCRIPTION
The three types of buffering available are unbuffered, block
buffered, and line buffered. When an output stream is
unbuffered,
information appears on the destination file or terminal as
soon as
written; when it is block buffered many characters are saved
up and
written as a block; when it is line buffered characters are
saved
up until a newline is encountered or input is read from
stdin.
Fflush (see fclose(3)) may be used to force the block out
early.
Normally all files are block buffered. A buffer is obtained
from
malloc(3) upon the first getc or putc(3) on the file. If the
standard stream stdout refers to a terminal it is line
buffered.
The standard stream stderr is always unbuffered.
The setvbuf function may be used
at any time on any open stream to
change its buffer. The mode parameter must be one of the
following
three macros:
_IONBF unbuffered
_IOLBF line buffered
_IOFBF fully buffered
Except for unbuffered files, the
buf argument should point to a
buffer at least size bytes long; this buffer will be used
instead
of the current buffer. If the argument buf is NULL, only the
mode
is affected; a new buffer will be allocated on the next read
or
write operation. The setvbuf function may be used at any
time, but
can only change the mode of a stream when it is not
"active":
that is, before any or immediately after a call to
fflush.
The other three calls are, in
effect, simply aliases for calls to
setvbuf. The setbuf function is exactly equivalent to the
call
setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
The setbuffer function is the
same, except that the size of the
buffer is up to the caller, rather than being determined by
the
default The setlinebuf function is exactly equivalent to the
call:
setvbuf(stream, (char *)NULL, _IOLBF, 0);
SEE ALSO
fopen(3), getc(3), putc(3), malloc(3), fclose(3), puts(3),
printf(3), fread(3)
BUGS
The setbuffer and setlinebuf functions are not portable to
non-
4.2BSD versions of UNIX. On 4.2BSD and 4.3BSD systems,
setbuf
always uses a suboptimal buffer size and should be avoided.
Setbuffer is not usually needed as the default file I/O
buffer
sizes are optimal.