[ Previous | Next | Contents | Glossary | Home | Search ]
AIX Version 4.3 Base Operating System and Extensions Technical Reference, Volume 2

setbuf, setvbuf, setbuffer, or setlinebuf Subroutine


Assigns buffering to a stream.


Standard C Library (libc.a)


#include <stdio.h>
void setbuf (StreamBuffer)
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;


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:

_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.


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.

Implementation Specifics

These subroutines are part of Base Operating System (BOS) Runtime.

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

Related Information

The fopen, freopen, or fdopen subroutine, fread subroutine, getc, fgetc, getchar, or getw subroutine, getwc, fgetwc, or getwchar subroutine, malloc, free, realloc, calloc, mallopt, mallinfo, or alloca subroutine, putc, putchar, fputc, or putw subroutine, putwc, putwchar, or fputwc subroutine.

The Input and Output Handling Programmer's Overview in AIX Version 4.3 General Programming Concepts: Writing and Debugging Programs.

[ Previous | Next | Contents | Glossary | Home | Search ]