Change the buffering scheme for a stream.
#include <stdio.h> int setvbuf(FILE *stream, char *buf, int mode, size_t size);
stream
A pointer to a FILE stream.
buf
A buffer for input and output
mode
A buffering mode.
size
The size of the buffer.
The setvbuf() function allows the manipulation of the buffering scheme as well as the size of the buffer that the stream uses. Call this function after opening the stream but before reading from or writing to it.
The buf argument is a pointer to a character array. The size argument specifies the size of the character array pointed to by buf. The most efficient buffer size is a multiple of BUFSIZ, defined in stdio.h.
If buf is a null pointer, then the library creates its own buffer of size bytes. The mode argument specifies the buffering scheme to use:
This function returns zero if it is successful and returns a nonzero value if it fails.This facility may not be available on some configurations of the EWL.
#include <stdio.h> #include <stdlib.h> int main(void) { FILE *f; char name[80]; printf("Enter the name of the file to write to.\n"); fgets(name, 80, stdin); if ((f = fopen(name, "w")) == NULL) { printf("Can't open file %s.\n", name); exit(1); } setvbuf(f, NULL, _IOLBF, BUFSIZ); fprintf(f, "This file is now\n"); fprintf(f, "line buffered.\n"); fclose(f); return 0; }