[Previous] [Contents] [Next]

setvbuf()

Associate a buffer with a stream

Synopsis:

#include <stdio.h>

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

Library:

libc

Description:

The setvbuf() function associates a buffer with the stream designated by fp. setvbuf() must be called after opening the stream, and before any reading or writing. The mode determines how the stream is buffered:

_IOFBF
Input and output are fully buffered.
_IOLBF
Output is line buffered (the buffer is flushed when a newline character is written, when the buffer is full, or when input is requested).
_IONBF
Input and output are completely unbuffered.

If buf isn't NULL, the buffer it points to is used instead of an automatically allocated buffer. The size argument specifies the size of the buffer.

Returns:

0
Success.
EINVAL
The mode argument isn't valid.
ENOMEM
The buf argument is NULL, size isn't 0, and there isn't enough memory available to allocate a buffer.

Examples:

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
    char *buf;
    FILE *fp;

    fp = fopen( "file", "r" );
    buf = malloc( 1024 );
    setvbuf( fp, buf, _IOFBF, 1024 );
    
    /* work with fp */
    ...
    
    fclose( fp );

    /* This is OUR buffer, so we have
     * to free it.  Do that AFTER
     * you've closed the file.
     */

    free( buf );
    return EXIT_SUCCESS;
}

Classification:

ANSI

Safety:
Cancellation point No
Interrupt handler No
Signal handler No
Thread Yes

See also:

fopen(), setbuf()


[Previous] [Contents] [Next]