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.

Chapter 4: The Standard I/O Library
Overview
The last chapter examined the low-level I/O interface provided by the UNIX operating system. Although, as you'll see later in the book, this interface is useful for a number of applications, it isn't very convenient to use for everyday programming.
To understand why, think about writing a program that computes your monthly budget. This program prompts you for budget items (strings) and monthly costs (numbers). It then performs some calculations and displays a nice table of values. The table contains the names of the budget items (strings) and several columns of numbers, nicely lined up at the decimal point. Sounds pretty simple, until you realize that you have to write not only the functions to compute your budget but also a function to read in a string up to a newline character, a function to convert strings of characters like 123.456 to numbers, a function to line up all the numbers in columns and print them out, and so forth. These functions aren't terribly difficult, but imagine having to write them for every program you develop—you'd be spending more time writing I/O formatting routines than you would actually writing your program!
Fortunately, the original developers of UNIX realized this too, and they developed a powerful set of functions called the Standard I/O Library. The primary purpose of the library is to separate the mechanics of coding I/O, so that you can spend your time writing “real” code instead of writing mundane things like string-to-integer conversion functions. Specifically, the library performs three major tasks for you:
 The library automatically buffers I/O. When reading or writing data, it is much more efficient to do so in large chunks, rather than one byte (or a few bytes) at a time. This is because each read or write request results in a call to the operating system and then usually initiates action on the part of some piece of hardware, such as a disk. Reading or writing one byte at a time to a disk drive is horrendously inefficient—for each byte, the operating system has to tell the disk to seek to some address, wait for the disk to do so, request the disk to transfer a byte to or from memory, wait for the disk to do so, and then return the result to your program. Imagine hundreds of programs doing this at the same time, each with thousands of bytes of data.
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.
 The library performs I/O conversions. As you know, data is stored in binary form inside a computer. For example, the decimal integer 1234 is stored internally (on a 32-bit system) in the following binary form:
    00000000 00000000 00000100 11010010
Floating-point numbers are even more unwieldy—the decimal number 1234.5678 is stored internally (on a system using the IEEE 754 floating-point format) in the following binary form:
    01000100 10011010 01010010 00101011
Because human beings don't think very well in binary, it is necessary to convert between the binary system used by the computer and the decimal system used by people. The Standard I/O Library provides a number of convenient ways to do this.
 The library can format I/O. Most programs that produce output intended to be read by humans make an effort to print their data in a format that is easy to read. For example, programs that produce large amounts of numerical data try to line that data up into columns, programs that produce lists try to make each line of the list line up somehow, and so forth. The Standard I/O Library makes it easy to perform these tasks.
The Standard I/O Library exists in pretty much the same form on all versions of UNIX, although some of the more obscure options vary from release to release. The version of the library discussed in this chapter is the one specified by the ANSI C standard.

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