HP C/iX Library Reference Manual (30026-90004)

304 Chapter5
HP C/iX Library Function Descriptions
setbuf
FILE *fp;
char buffer[BUFSIZ];
fp = fopen(argv[1], "r");
setbuf(fp, buffer);
This fragment shows the correct order of events. First, the file is opened, and the buffering
is assigned using setbuf(). From that point on any input taken from fp is buffered
through the array
buffer.
Buffering can be eliminated altogether by specifying the null
pointer in place of the buffer name, as in
setbuf(fp, NULL);
This causes input or output using fp to be completely unbuffered.
The setbuf function is limited to buffer sizes of either BUFSIZ bytes or zero. setbuf()
assumes that the character array pointed to by
buffer
is BUFSIZ bytes. Passing setbuf()
a (non-null) pointer to a smaller array can cause severe problems during operation because
the standard I/O functions might overwrite memory following the end of the buffer.
See Also
setvbuf(), fopen(), getc(), malloc(), putc(), ANSI C 4.9.5.5, POSIX.1 8.1