(BSD System Compatibility)

setbuffer(3bsd)


setbuffer, setlinebuf -- (BSD) assign buffering to a stream

Synopsis

   /usr/ucb/cc [flag . . . ] file . . .
   

#include <stdio.h>

setbuffer(FILE *stream, char *buf, int size);

setlinebuf(FILE *stream);

Description

The three types of buffering available are unbuffered, block buffered, and line buffered. When an output stream is unbuffered, information appears on the destination file or terminal as soon as written; when it is block buffered many characters are saved up and written as a block; when it is line buffered characters are saved up until a NEWLINE is encountered or input is read from any line buffered input stream. fflush [see fclose(3S)] may be used to force the block out early. Normally all files are block buffered. A buffer is obtained from malloc(3C) upon the first getc or putc(3S) on the file.

By default, output to a terminal is line buffered, except for output to the standard stream stderr which is unbuffered, and all other input/output is fully buffered.

setbuffer can be used after a stream has been opened but before it is read or written. It uses the character array buf whose size is determined by the size argument instead of an automatically allocated buffer. If buf is the NULL pointer, input/output will be completely unbuffered. A manifest constant BUFSIZ, defined in the stdio.h header file, tells how big an array is needed:

   char buf[BUFSIZ];

setlinebuf is used to change the buffering on a stream from block buffered or unbuffered to line buffered. Unlike setbuffer, it can be used at any time that the file descriptor is active.

A file can be changed from unbuffered or line buffered to block buffered by using freopen [see fopen(3S)]. A file can be changed from block buffered or line buffered to unbuffered by using freopen followed by setbuffer with a buffer argument of NULL.

References

fclose(3S), fopen(3S), fprintf(3S), fread(3S), getc(3S), malloc(3C), putc(3S), puts(3S), setbuf(3S)

Notices

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.
© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 25 April 2004