HP C/iX Library Reference Manual (30026-90004)
Chapter 5 303
HP C/iX Library Function Descriptions
setbuf
setbuf
Assigns a buffer to an open stream.
Syntax
#include <stdio.h>
void setbuf (FILE *
stream
, char *
buffer
);
Parameters
stream
A pointer to an open stream.
buffer
Either a pointer to a character array for buffered I/O, or null for
unbuffered I/O.
Return Values
None.
Description
Normally, a standard I/O buffer is obtained through a call to malloc() on the first call to
getc() or putc() (which all I/O functions eventually call). The standard I/O system
normally buffers I/O in a buffer which is BUFSIZ bytes long. Exceptions are stdout, which,
when directed to a terminal, is line buffered, and stderr, which is normally unbuffered.
NOTE
Using an automatic array as a standard I/O buffer can be dangerous.
Automatic variables are only defined in the code block in which they are
declared. Thus, buffering which relies on an automatic array is only in effect
during the current code block (main program or function). If you pass a file
pointer to another function, and the stream pointed to by that file pointer is
buffered using an automatic array, memory faults or other errors can occur.
Therefore, if you use an automatic array for stream buffering, the stream
should be used and closed only in the code block containing the array
declaration. To avoid this restriction, use global or static arrays for buffering:
char buffer[BUFSIZ];
…
main()
{
…
setbuf (fp, buffer);
}
The setbuf function enables you to change the buffer used for all standard I/O functions.
The following example of a code fragment causes the array
buffer
to be used for buffering:
…