Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer


Controls stream buffering and buffer size.

int setvbuf(
   FILE *stream,
   char *buffer,
   int mode,
   size_t size 


Pointer to FILE structure.
User-allocated buffer.
Mode of buffering.
Buffer size in bytes. Allowable range: 2 <= size <= INT_MAX (2147483647). Internally, the value supplied for size is rounded down to the nearest multiple of 2.

Return Value

Returns 0 if successful, or a nonzero value if an illegal type or buffer size is specified.


The setvbuf function allows the program to control both buffering and buffer size for stream. stream must refer to an open file that has not undergone an I/O operation since it was opened. The array pointed to by buffer is used as the buffer, unless it is NULL, in which case setvbuf uses an automatically allocated buffer of length size/2 * 2 bytes.

The mode must be _IOFBF, _IOLBF, or _IONBF. If mode is _IOFBF or _IOLBF, then size is used as the size of the buffer. If mode is _IONBF, the stream is unbuffered and size and buffer are ignored. Values for mode and their meanings are:

Full buffering; that is, buffer is used as the buffer and size is used as the size of the buffer. If buffer is NULL, an automatically allocated buffer size bytes long is used.
For some systems, this provides line buffering. However, for Win32, the behavior is the same as _IOFBF - Full Buffering.
No buffer is used, regardless of buffer or size.


Routine Required header Compatibility
setvbuf <stdio.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.


// crt_setvbuf.c
/* This program opens two streams: stream1
 * and stream2. It then uses setvbuf to give stream1 a
 * user-defined buffer of 1024 bytes and stream2 no buffer.

#include <stdio.h>

int main( void )
   char buf[1024];
   FILE *stream1, *stream2;

   if( ((stream1 = fopen( "data1", "a" )) != NULL) &&
       ((stream2 = fopen( "data2", "w" )) != NULL) )
      if( setvbuf( stream1, buf, _IOFBF, sizeof( buf ) ) != 0 )
         printf( "Incorrect type or size of buffer for stream1\n" );
         printf( "'stream1' now has a buffer of 1024 bytes\n" );
      if( setvbuf( stream2, NULL, _IONBF, 0 ) != 0 )
         printf( "Incorrect type or size of buffer for stream2\n" );
         printf( "'stream2' now has no buffer\n" );


'stream1' now has a buffer of 1024 bytes
'stream2' now has no buffer

See Also

Stream I/O Routines | fclose | fflush | fopen | setbuf | Run-Time Routines and .NET Framework Equivalents

© 2015 Microsoft