Name
setbuf
Synopsis
Sets up I/O buffering for an open file
#include <stdio.h> voidsetbuf
( FILE * restrictfp
, char * restrictbuffer
);
The setbuf()
function is
similar to setvbuf()
, except that
it has no return value, and no parameters to specify a buffering
mode or a buffer size. The size of the buffer established by
setbuf()
is given by the value of
the macro BUFSIZ
. If the
buffer
argument is not a null pointer,
the setbuf()
call initiates fully
buffered input and output for the specified file, so that the buffer
is filled completely before data appears from the source or at the
destination; this behavior corresponds to the buffering mode
specified by the macro _IOFBF
as
the mode
argument to setvbuf()
. If the
buffer
argument is a null pointer,
setbuf()
disables all I/O
buffering for the file, so that data is written and read
directly.
You may call the setbuf()
function only after the file has been successfully opened, and
before any file I/O operations have taken place.
Example
FILE *fp = tmpfile();
unsigned char *iobuffer = malloc( BUFSIZ );
if ( iobuffer != NULL )
{setbuf
( fp, iobuffer ); // Make sure temporary file is buffered.
}
/* ... now write and read the temporary file as needed ... */
Get C in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.