setbuf(3S) setbuf(3S)
NAME
setbuf, setvbuf, setbuffer, setlinebuf - assign buffering to
a stream
SYNOPSIS
#include <stdio.h>
void setbuf (stream, buf)
FILE *stream;
char *buf;
SYNOPSIS (AT&T System V)
int setvbuf (stream, buf, type, size)
FILE *stream;
char *buf;
int type, size;
SYNOPSIS (4.2 BSD)
void setbuffer (stream, buf, size)
FILE *stream;
char *buf;
int size;
void 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(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 always unbuf-
fered.
setbuf may 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 constant BUFSIZ, defined in the <stdio.h> header file,
tells how big an array is needed:
char buf[BUFSIZ];
Page 1 CX/UX Programmer's Reference Manual
setbuf(3S) setbuf(3S)
Setvbuf may 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:
_IOFBF causes input/output to be fully buffered.
_IOLBF causes output to be line buffered; the buffer will
be flushed when a newline is written, the buffer is
full, or input is requested.
_IONBF causes input/output to be completely unbuffered.
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.
The constant BUFSIZ in <stdio.h> is suggested as a good
buffer size. If input/output is unbuffered, buf and size
are ignored.
Setbuffer, an alternate form of setbuf, may be used after a
stream has been opened but before it is read or written.
The character array buf whose size is determined by the size
argument is used instead of an automatically allocated
buffer. If buf is the constant pointer NULL, input/output
will be completely unbuffered.
Setlinebuf may be used to change stdout or stderr from block
buffered or unbuffered to line buffered. Unlike setbuf and
setbuffer it can be used at any time that the file descrip-
tor 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 unbuf-
fered by using freopen followed by setbuf with a buffer
argument of NULL.
DIAGNOSTICS
If an illegal value for type or size is provided, setvbuf
returns a non-zero value. In programs linked in one of the
ANSI C compilation modes (see hc(1)), and in programs linked
in the 88open OCS-compliant mode, setvbuf also returns a
non-zero value when the request cannot otherwise be honored.
In all other cases, the value returned will be zero.
SEE ALSO
fopen(3S), getc(3S), malloc(3C), putc(3S), stdio(3S), hc(1).
NOTE
A common source of error is allocating buffer space as an
''automatic'' variable in a code block, and then failing to
Page 2 CX/UX Programmer's Reference Manual
setbuf(3S) setbuf(3S)
close the stream in the same block.
The order of the buf and type parameters to setvbuf was
reversed in System V Release 3.0 to be consistent with the
System V Interface Definition. Source code written for AT&T
System V Release 2 or CX/UX Release 3 must be changed before
linking.
Page 3 CX/UX Programmer's Reference Manual