Add Book to My BookshelfPurchase This Book Online

Chapter 4 - The Standard I/O Library

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

Buffering
As mentioned previously, the Standard I/O Library buffers I/O internally. There are a number of quirks to the way things get buffered, which causes some inconsistencies. The quirks exist in an attempt to make the library “do the right thing” under all circumstances:
 Disk files, both for reading and writing, are buffered in large chunks (usually 1,024 bytes or more).
 The stdout stream is line-buffered if it refers to a terminal device; otherwise, it is buffered like a disk file. Therefore, when stdout refers to a terminal, the buffer is flushed each time a newline character is printed.
 The stderr stream is completely unbuffered (except on some BSD-based systems, where it is line-buffered). This causes writes to stderr to appear immediately. This is necessary to allow errors to show up even when a program fails and dumps core. If the writes were buffered, they would not be flushed before the program was terminated.
 If the stdin stream refers to a terminal device, the stdout stream is flushed automatically whenever a read from stdin is performed. This allows prompts (which typically do not contain newline characters) to appear.
 A call to fseek or rewind flushes any write buffers that contain outstanding data.
Usually, the library does what is expected (the “principle of least surprise”). However, there are situations in which the library's default behavior is not good enough. Thus, a number of routines are provided for overriding the library's buffering decisions:
    #include <stdio.h>
    int fflush(FILE *stream);
    void setbuf(FILE *stream, char *buf);
    void setvbuf(FILE *stream, char *buf, int type, size_t size);
If stream is open for writing, fflush causes any buffered data waiting to be written to the file. If stream is open for reading, fflush causes any unread data in the buffer to be discarded. If stream is NULL, fflush flushes data to disk for all streams that are open for writing.
Use the setbuf function after a stream is opened but before it is read or written. It causes the array pointed to by buf (which should be of size BUFSIZ) to be used instead of an automatically allocated buffer. If buf is NULL, the stream is completely unbuffered.
Use the setvbuf function after a stream is opened but before it is read or written. The type argument indicates how stream is buffered, using the following values:
_IOFBF
Fully buffers I/O.
_IOLBF
Line buffers output. The output is flushed when a newline is written, the buffer is full, or input is requested.
_IONBF
Completely unbuffers I/O.
If buf is not NULL, the array it points to is used for buffering instead of an automatically allocated buffer. In this case, size specifies the size of buf in bytes.
Porting Notes
BSD UNIX provides two other buffering functions, setbuffer and setlinebuf. The setbuffer function is like setbuf, except that it also allows the size of the buffer to be specfied. You can replace it with the following call:
    setvbuf(stream, buf, _IOFBF, sizeof(buf));
The setlinebuf function changes a stream to be line-buffered; it can be used any time the stream is active. You can replace it with the following call, which must be made before the stream is read or written:
    setvbuf(stream, NULL, _IOLBF, 0);

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback