SETBUF(3V) — UNKNOWN SECTION OF THE MANUAL
NAME
setbuf, setbuffer, setlinebuf, setvbuf − assign buffering to a stream
SYNOPSIS
#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. Normally all files are block buffered. A buffer is obtained from malloc(3) upon the first getc or putc(3S) on the file.
By default, output to a terminal is line buffered and all other input/output is fully buffered.
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 setbuf, can be used after a stream has been opened but before it is read or written. It causes the character array buf whose size is determined by the size argument to be used 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:
_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.
setlinebuf is used to change the buffering on a stream from block buffered or unbuffered 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.
SEE ALSO
fopen(3V), getc(3S), putc(3S), malloc(3), fclose(3S), puts(3S), printf(3V), fread(3V), setbuf(3S)
DIAGNOSTICS
If an illegal value for type or size is provided, setvbuf returns a non-zero value. Otherwise, the value returned will be zero.
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.
Sun Release 3.2 — Last change: 16 April 1986