setbuffer(3C) manual page
Table of Contents
setbuffer, setlinebuf - assign buffering to a stream
#include
<stdio.h>
void setbuffer(FILE *iop, char *abuf, size_t asize);
void setlinebuf(FILE
*iop);
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
.
malloc(3C)
, fclose(3S)
, fopen(3S)
,
fread(3S)
, getc(3S)
, printf(3S)
, putc(3S)
, puts(3S)
, setbuf(3S)
, setvbuf(3S)
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.
Table of Contents