After opening a stream (but before any other operations have been
performed on it), you can explicitly specify what kind of buffering you
want it to have using the setvbuf
function.
The facilities listed in this section are declared in the header file `stdio.h'.
_IOFBF
(for full buffering), _IOLBF
(for line buffering), or
_IONBF
(for unbuffered input/output).
If you specify a null pointer as the buf argument, then setvbuf
allocates a buffer itself using malloc
. This buffer will be freed
when you close the stream.
Otherwise, buf should be a character array that can hold at least
size characters. You should not free the space for this array as
long as the stream remains open and this array remains its buffer. You
should usually either allocate it statically, or malloc
(see section Unconstrained Allocation) the buffer. Using an automatic array
is not a good idea unless you close the file before exiting the block
that declares the array.
While the array remains a stream buffer, the stream I/O functions will use the buffer for their internal purposes. You shouldn't try to access the values in the array directly while the stream is using it for buffering.
The setvbuf
function returns zero on success, or a nonzero value
if the value of mode is not valid or if the request could not
be honored.
setvbuf
function to
specify that the stream should be fully buffered.
setvbuf
function to
specify that the stream should be line buffered.
setvbuf
function to
specify that the stream should be unbuffered.
setvbuf
. This value is
guaranteed to be at least 256
.
The value of BUFSIZ
is chosen on each system so as to make stream
I/O efficient. So it is a good idea to use BUFSIZ
as the size
for the buffer when you call setvbuf
.
Actually, you can get an even better value to use for the buffer size
by means of the fstat
system call: it is found in the
st_blksize
field of the file attributes. See section The meaning of the File Attributes.
Sometimes people also use BUFSIZ
as the allocation size of
buffers used for related purposes, such as strings used to receive a
line of input with fgets
(see section Character Input). There is no
particular reason to use BUFSIZ
for this instead of any other
integer, except that it might lead to doing I/O in chunks of an
efficient size.
setvbuf
with a mode argument of
_IONBF
. Otherwise, it is equivalent to calling setvbuf
with buf, and a mode of _IOFBF
and a size
argument of BUFSIZ
.
The setbuf
function is provided for compatibility with old code;
use setvbuf
in all new programs.
This function is provided for compatibility with old BSD code. Use
setvbuf
instead.
This function is provided for compatibility with old BSD code. Use
setvbuf
instead.
It is possible to query whether a given stream is line buffered or not using a non-standard function introduced in Solaris and available in the GNU C library.
__flbf
function will return a nonzero value in case the
stream stream is line buffered. Otherwise the return value is
zero.
This function is declared in the `stdio_ext.h' header.
Two more extensions allow to determine the size of the buffer and how much of it is used. These functions were also introduced in Solaris.
__fbufsize
function return the size of the buffer in the
stream stream. This value can be used to optimize the use of the
stream.
This function is declared in the `stdio_ext.h' header.
__fpending
This function is declared in the `stdio_ext.h' header.
Go to the first, previous, next, last section, table of contents.