setbuf, setvbuf, setbuffer, or setlinebuf Subroutine

Purpose

Assigns buffering to a stream.

Library

Standard C Library (libc.a)

Syntax

#include <stdio.h>

void setbuf ( Stream Buffer)
FILE *Stream;
char *Buffer;

int setvbuf (Stream, Buffer, Mode, Size)
FILE *Stream;
char *Buffer;
int  Mode;
size_t  Size;

void setbuffer (Stream, Buffer, Size)
FILE *Stream;
char *Buffer;
size_t Size;
void setlinebuf (Stream)
FILE *Stream;

Description

The setbuf subroutine causes the character array pointed to by the Buffer parameter to be used instead of an automatically allocated buffer. Use the setbuf subroutine after a stream has been opened, but before it is read or written.

If the Buffer parameter is a null character pointer, input/output is completely unbuffered.

A constant, BUFSIZ, defined in the stdio.h file, tells how large an array is needed:

char buf[BUFSIZ];

For the setvbuf subroutine, the Mode parameter determines how the Stream parameter is buffered:

Item Description
_IOFBF Causes input/output to be fully buffered.
_IOLBF Causes output to be line-buffered. The buffer is flushed when a new line is written, the buffer is full, or input is requested.
_IONBF Causes input/output to be completely unbuffered.

If the Buffer parameter is not a null character pointer, the array it points to is used for buffering. The Size parameter specifies the size of the array to be used as a buffer, but all of the Size parameter's bytes are not necessarily used for the buffer area. The constant BUFSIZ in the stdio.h file is one buffer size. If input/output is unbuffered, the subroutine ignores the Buffer and Size parameters. The setbuffer subroutine, an alternate form of the setbuf subroutine, is used after Stream has been opened, but before it is read or written. The character array Buffer, whose size is determined by the Size parameter, is used instead of an automatically allocated buffer. If the Buffer parameter is a null character pointer, input/output is completely unbuffered.

The setbuffer subroutine is not needed under normal circumstances because the default file I/O buffer size is optimal.

The setlinebuf subroutine is used to change the stdout or stderr file from block buffered or unbuffered to line-buffered. Unlike the setbuf and setbuffer subroutines, the setlinebuf subroutine can be used any time Stream is active.

A buffer is normally obtained from the malloc subroutine at the time of the first getc subroutine or putc subroutine on the file, except that the standard error stream, stderr, is normally not buffered.

Output streams directed to terminals are always either line-buffered or unbuffered.

Note: A common source of error is allocating buffer space as an automatic variable in a code block, and then failing to close the stream in the same block.

The setbuffer and setlinebuf subroutines are included for compatibility with Berkeley System Distribution (BSD).

Parameters

Item Description
Stream Specifies the input/output stream.
Buffer Points to a character array.
Mode Determines how the Stream parameter is buffered.
Size Specifies the size of the buffer to be used.

Return Values

Upon successful completion, setvbuf returns a value of 0. Otherwise it returns a nonzero value if a value that is not valid is given for type, or if the request cannot be honored.