setbuf(3) — Subroutines
OSF
NAME
setbuf, setvbuf, setbuffer, setlinebuf − Assigns buffering to a stream
LIBRARY
Standard I/O Package (libc.a)
SYNOPSIS
#include <stdio.h> void setbuf (
FILE ∗stream,
char ∗buffer ); int setvbuf (
FILE ∗stream,
char ∗buffer,
int mode,
size_t size ); void setbuffer (
FILE ∗stream,
char ∗buffer,
char ∗size ); void setlinebuf (
FILE ∗stream );
PARAMETERS
streamSpecifies the input/output stream.
bufferPoints to a character array.
modeDetermines how the stream parameter is buffered.
sizeSpecifies the size of the buffer to be used.
DESCRIPTION
The setbuf() function causes the character array pointed to by the buffer parameter to be used instead of an automatically allocated buffer. Use the setbuf() function 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 header file, tells how large an array is needed: char buf[BUFSIZ];
For the setvbuf() function, the mode parameter determines how the stream parameter is buffered:
_IOFBFCauses input/output to be fully buffered.
_IOLBFCauses output to be line-buffered. The buffer is flushed when a new line is written, the buffer is full, or input is requested.
_IONBUFCauses 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 instead of an automatically allocated buffer. The size parameter specifies the size of the buffer to be used. The constant BUFSIZ in the stdio.h header file is one buffer size. If input/output is unbuffered, the buffer and size parameters are ignored. The setbuffer() function, an alternate form of the setbuf() function, 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() function is not needed under normal circumstances since the default file I/O buffer size is optimal.
The setlinebuf() function is used to change stdout or stderr from block-buffered or unbuffered to line-buffered. Unlike the setbuf() and setbuffer() functions, the setlinebuf() function can be used any time the file descriptor is active.
A buffer is normally obtained from the malloc() function at the time of the first getc() or putc() function 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.
NOTES
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.
AES Support Level:
Full use (setbuf(), setvbuf())
RELATED INFORMATION
Functions: fopen(3), fread(3), getc(3), getwc(3), malloc(3), putc(3), putwc(3)