Syntax
#include <stdio.h> int setvbuf(FILE *stream, char *buf, int type, size_t size);Description
setvbuf allows control over the buffering strategy and buffer size for a specified stream. The stream must refer to a file that has been opened, but not read or written to. The array pointed to by buf designates an area that you provide that the C library may choose to use as a buffer for the stream. A buf value of NULL indicates that no such area is supplied and that the C library is to assume responsibility for managing its own buffers for the stream. If you supply a buffer, it must exist until the stream is closed.
The type must be one of the following: compact break=fit.
Value
If type is _IOFBF or _IOLBF, size is the size of the supplied buffer. If buf is NULL, the C library takes size as the suggested size for its own buffer. If type is _IONBF, both buf and size are ignored.
The value for size must be greater than 0.
setvbuf returns 0 if successful. It returns nonzero if an invalid value was specified in the parameter list, or if the request cannot be performed.
Warning: The array used as the buffer must still exist when the specified stream is closed. For example, if the buffer is declared within the scope of a function block, the stream must be closed before the function is terminated and frees the storage allocated to the buffer.
This example sets up a buffer of buf for stream1 and specifies that input to stream2 is to be unbuffered.
#include <stdio.h>#include <stdlib.h>
#define  BUF_SIZE      1024
#define  FILE1         "setvbuf1.dat"
#define  FILE2         "setvbuf2.dat"
char buf[BUF_SIZE];
FILE *stream1,*stream2;
int main(void)
{
   int flag = EXIT_SUCCESS;
   stream1 = fopen(FILE1, "r");
   stream2 = fopen(FILE2, "r");
   /* stream1 uses a user-assigned buffer of BUF_SIZE bytes                   */
   if (setvbuf(stream1, buf, _IOFBF, sizeof(buf)) != 0) {
      printf("Incorrect type or size of buffer\n");
      flag = EXIT_FAILURE;
   }
   /* stream2 is unbuffered                                                   */
   if (setvbuf(stream2, NULL, _IONBF, 0) != 0) {
      printf("Incorrect type or size of buffer\n");
      flag = EXIT_FAILURE;
   }
   fclose(stream1);
   fclose(stream2);
   return  flag;
}
Related Information