Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

5


StandardI/O Library


5.1 Introduction


In this chapter, we describe the standardI/O library.This library is specified by the ISO
Cstandardbecause it has been implemented on many operating systems other than the
UNIX System. Additional interfaces aredefined as extensions to the ISO C standardby
the Single UNIX Specification.
The standardI/O library handles such details as buffer allocation and performing
I/O in optimal-sized chunks, obviating our need to worry about using the correct block
size (as in Section 3.9). This makes the library easy to use, but at the same time
introduces another set of problems if we’renot cognizant of what’s going on.

The standardI/O library was written by Dennis Ritchie around 1975. It was a major revision
of the Portable I/O library written by Mike Lesk. Surprisingly little has changed in the
standardI/O library after morethan 35 years.

5.2 Streams and FILEObjects


In Chapter 3, all the I/O routines centered on file descriptors. When a file is opened, a
file descriptor is returned, and that descriptor is then used for all subsequent I/O
operations. With the standardI/O library,the discussion centers onstreams.(Do not
confuse the standardI/O termstreamwith the STREAMS I/O system that is part of
System V and was standardized in the XSI STREAMS option in the Single UNIX
Specification, but is now marked obsolescent in SUSv4.) When we open or create a file
with the standardI/O library, we say that we have associated a stream with the file.
With the ASCII character set, a single character is represented by a single byte.With
international character sets, a character can be represented by morethan one byte.

143
Free download pdf