C setvbuf()

« Previous Function Next Function »


The setvbuf() function allows the programmer to specify a buffer, its size, and its mode for the specified stream. The character array pointed to by buf is used as the buffer for I/O operations on stream. The size of the buffer is set by the size, and the mode determines how buffering will be handled. In case if buf is null then setvbuf() will allocate its own buffer.

The legal values of mode are _IOFBF, _IONBF, and _IOLBF. These are defined in <stdio.h>.

When mode is set to _IOFBF, then full buffering will take place. And if mode is set to _IOLBF, then the stream will be line buffered. For the output stream, this means that buffer will be flushed each time a newline character is written. And the buffer also flushed when full. For input stream, the input is buffered until a newline is read. And if mode is _IONBF, then no buffering takes place.

setvbuf() Syntax

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

The setvbuf() function returns zero on success, otherwise returns non-zero on failure.

setvbuf() Example

Following c code sets the stream fp to line-buffered mode with a buffer size of 128 :

char buffer[128];
setvbuf(fp, buffer, _IOLBF, 128);

« Previous Function Next Function »

Latest 5 Posts by CodesCracker


Quick Links
Signup - Login - Give Online Test