This documentation is archived and is not being maintained.


Controls stream buffering.

void setbuf(
   FILE *stream,
   char *buffer 


Pointer to FILE structure.
User-allocated buffer.


The setbuf function controls buffering for stream. The stream argument must refer to an open file that has not been read or written. If the buffer argument is NULL, the stream is unbuffered. If not, the buffer must point to a character array of length BUFSIZ, where BUFSIZ is the buffer size as defined in STDIO.H. The user-specified buffer, instead of the default system-allocated buffer for the given stream, is used for I/O buffering. The stderr stream is unbuffered by default, but you can use setbuf to assign buffers to stderr.

setbuf has been replaced by setvbuf, which is the preferred routine for new code. setbuf is retained for compatibility with existing code.


Routine Required header Compatibility
setbuf <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_setbuf.c
/* This program first opens files named DATA1 and
 * DATA2. Then it uses setbuf to give DATA1 a user-assigned
 * buffer and to change DATA2 so that it has no buffer.

#include <stdio.h>

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

   if( ((stream1 = fopen( "data1", "a" )) != NULL) &&
       ((stream2 = fopen( "data2", "w" )) != NULL) )
      /* "stream1" uses user-assigned buffer: */
      setbuf( stream1, buf );
      printf( "stream1 set to user-defined buffer at: %Fp\n", buf );

      /* "stream2" is unbuffered              */
      setbuf( stream2, NULL );
      printf( "stream2 buffering disabled\n" );

Sample Output

stream1 set to user-defined buffer at: 0012FCDC
stream2 buffering disabled

See Also

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