setbuf(3S-BSD) MISC. REFERENCE MANUAL PAGES setbuf(3S-BSD)
NAME
setbuf, setbuffer, setlinebuf, setvbuf - assign buffering to
a stream
SYNOPSIS
cc [ flag... ] file ... -lucb
#include <stdio.h>
setbuf(stream, buf)
FILE *stream;
char *buf;
setbuffer(stream, buf, size)
FILE *stream;
char *buf;
int size;
setlinebuf(stream)
FILE *stream;
int setvbuf(stream, buf, type, size)
FILE *stream;
char *buf;
int type, size;
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(3S)) may be used to force the block out early. Nor-
mally all files are block buffered. A buffer is obtained
from malloc(3C) upon the first getc or putc(3S) on the file.
If the standard stream stdout refers to a terminal it is
line buffered. The standard stream stderr is unbuffered by
default. setbuf can be used after a stream has been opened
but before it is read or written. It causes the array
pointed to by buf to be used instead of an automatically
allocated buffer. If buf is the NULL pointer, input/output
will be completely unbuffered. A manifest constant BUFSIZ,
defined in the <stdio.h> header file, tells how big an array
is needed:
char buf[BUFSIZ]; setbuffer, an alternate form of set-
buf, can be used after a stream has been opened but
before it is read or written. It uses the character
array buf whose size is determined by the size argument
instead of an automatically allocated buffer. If buf
is the NULL pointer, input/output will be completely
unbuffered. setvbuf can be used after a stream has
been opened but before it is read or written. type
determines how stream will be buffered. Legal values
for type (defined in <stdio.h>) are:
1
setbuf(3S-BSD) MISC. REFERENCE MANUAL PAGES setbuf(3S-BSD)
IOFBF
fully buffers the input/output.
IOLBF
line buffers the output; the buffer will be flushed
when a NEWLINE is written, the buffer is full, or
input is requested.
IONBF
completely unbuffers the input/output. If buf is
not the NULL pointer, the array it points to will be
used for buffering, instead of an automatically
allocated buffer. size specifies the size of the
buffer to be used. setlinebuf is used to change the
buffering on a stream from block buffered or unbuf-
fered to line buffered. Unlike setbuf, setbuffer,
and setvbuf, it can be used at any time that the
file descriptor is active. A file can be changed
from unbuffered or line buffered to block buffered
by using freopen (see fopen(3S)). A file can be
changed from block buffered or line buffered to
unbuffered by using freopen followed by setbuf with
a buffer argument of NULL.
NOTE
A common source of error is allocating buffer space as an
``automatic'' variable in a code block, and then failing to
close the stream in the same block.
SEE ALSO
fclose(3S), fopen(3S), fread(3S), getc(3S), malloc(3C),
printf(3S), putc(3S), puts(3S), setbuf(3S) in the
Programmer's Reference Manual.
RETURN VALUE
If an illegal value for type or size is provided, setvbuf
returns a non-zero value. Otherwise, the value returned
will be zero.
2