associate a buffer with a file


#include <stdio.h>
int setvbuf( FILE *fp,
             char *buf,
             int mode,
             size_t size );


The setvbuf() function can be used to associate a buffer with the file designated by fp. If this function is used, it must be called after the file has been opened, and before it has been read or written. The argument mode determines how the file fp will be buffered, as follows:

causes input/output to be fully buffered.
causes output to be line buffered (the buffer will be flushed when a newline character is written, when the buffer is full, or when input is requested.
causes input/output to be completely unbuffered.

If the argument buf isn't NULL, the array it points to will be used instead of an automatically allocated buffer. The argument size specifies the size of the array.


Zero on success, or a nonzero value if an invalid value is given for mode or size.


#include <stdio.h>
#include <stdlib.h>

void main()
  char *buf;
  FILE *fp;

  fp = fopen( "file", "r" );
  buf = malloc( 1024 );
  setvbuf( fp, buf, _IOFBF, 1024 );



Interrupt handler No
Signal handler No
Thread Yes

See also:

fopen(), setbuf()