Table of Contents

setvbuf

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

Controls buffering for stream stream. mode is _IOFBF for full buffering, _IOLBF for line buffering, _IONBF for no buffering. Non-null buf specifies buffer of size size to be used; otherwise, a buffer is allocated. Returns non-zero on error. Call must be before any other operation on stream.

    _IOLBF   File will buffered line by line
    _IONBF   Input/Output will not be buffered
    _IOFBF   Input/Output will fully buffered

C Sourcecode Example

/* 
 * setvbuf example code
 * http://code-reference.com/c/stdio.h/setvbuf
 */
 
#include <stdio.h> /* including standard library */
//#include <windows.h> /* uncomment this for Windows */
 
 
int main ( void )
{
  FILE *stream_not_buffered, *stream_full_buffered, *stream_line_buffered;
  stream_full_buffered=fopen ("test.txt","w");
  stream_not_buffered=fopen("test2.txt","w");
  stream_line_buffered=fopen("test3.txt","w");
 
/* case 1 full buffered */
  setvbuf ( stream_full_buffered , NULL , _IOFBF , 2000L );
 
  fputs("Buffered input for setvbuf full buffered", stream_full_buffered);
  fflush(stream_full_buffered);
  fclose(stream_full_buffered);
 
 
/* case 2 not buffered */
  setvbuf ( stream_not_buffered , NULL , _IONBF , BUFSIZ );
 
  fputs("input for setvbuf not buffered", stream_not_buffered);
  fflush(stream_not_buffered);
  fclose(stream_not_buffered);
 
 
/* case 3 line buffered */
  setvbuf ( stream_line_buffered , NULL , _IOLBF , 80 );
 
  fputs("Buffered input for setvbuf line buffered", stream_line_buffered);
  fflush(stream_line_buffered);
  fclose(stream_line_buffered);
 
 
  return 0;
}
  output: ./setvbuf
  

content of test.txt

Buffered input for setvbuf full buffered

content of test2.txt

input for setvbuf not buffered

content of test3.txt

Buffered input for setvbuf line buffered

see also setbuf