 | | By buffering reads and writes, the Standard I/O Library makes programs more efficient. When a program reads a single character, the library routine actually reads a large bufferful of characters (using read) and then returns the first character in the buffer to the program. The next several one-character reads are filled from the same buffer, without making any request to the operating system (or to a device such as a disk drive). When the program uses the entire buffer, the next one-character read causes the library to read another bufferful of characters, and so forth. Thus, assuming a buffer size of one Kbyte (1,024 characters), a program can read a ten-Kbyte file a character at a time with only ten calls to the operating system's read function, instead of 10,240 calls. Writes are handled in a similar fashion. Each time the program writes some data, the library routines transfer that data to a buffer. When the buffer fills up, it is written out using write and a new buffer is started. All of this happens invisibly to you, the programmer. | |
|