setbuffer(3B)
NAME
setbuffer, setlinebuf − assign buffering to a stream
SYNOPSIS
/usr/ucb/cc [ flag ... ] file ...
#include <stdio.h>
void setbuffer(iop, abuf, asize)
register FILE ∗iop;
char ∗abuf;
int asize;
int setlinebuf(iop)
register FILE ∗iop;
DESCRIPTION
setbuffer, setlinebuf − assign buffering to a stream 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(3S) may be used to force the block out early. Normally all files are block buffered. A buffer is obtained from malloc(3C) upon the first getc(3S) 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.
setbuffer() can be used after a stream, iop, has been opened but before it is read or written. It uses the character array abuf whose size is determined by the asize argument instead of an automatically allocated buffer. If abuf is the NULL pointer, input/output will be completely unbuffered. A manifest constant BUFSIZ , defined in the <stdio.h> header, tells how big an array is needed:
char buf[BUFSIZ];
setlinebuf() is used to change the buffering on a stream from block buffered or unbuffered to line buffered. Unlike setbuffer(), it can be used at any time that the stream, iop, is active.
A stream can be changed from unbuffered or line buffered to block buffered by using freopen(3S). A stream can be changed from block buffered or line buffered to unbuffered by using freopen(3S) followed by setbuf(3S) with a buffer argument of NULL.
SEE ALSO
malloc(3C), fclose(3S), fopen(3S), fread(3S), getc(3S), printf(3S), putc(3S), puts(3S), setbuf(3S)
NOTES
Use of these interfaces should be restricted to only applications written on BSD platforms. Use of these interfaces with any of the system libraries or in multi-thread applications is unsupported.
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 Microsystems — Last change: 05 Mar 1993