SETBUF(3V)

NAME
setbuf, setbuffer, setlinebuf, setvbuf - 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;

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(3V)) may be used to force the block out early. A buffer is obtained from malloc(3V) upon the first getc(3V) or putc(3S) on the file. By default, output to a terminal is line buffered, except for output to the standard stream stderr which is unbuffered. 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 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:

_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 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(3V)). 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.

SYSTEM V DESCRIPTION
If buf is not NULL and stream refers to a terminal device, setbuf() sets stream for line buffered input/output.

RETURN VALUES

setlinebuf() returns no useful value.

setvbuf() returns 0 on success. If an illegal value for type or size is provided, setvbuf() returns a non-zero value. setvbuf()

SEE ALSO
fclose(3V), fopen(3V), fread(3S), getc(3V), malloc(3V), printf(3V), putc(3S), puts(3S)

NOTES

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.