I want to introduce the concept of file I/O at this point because most
practical programs deal with files. The disadvantage of introducing
this concept now is that, strictly speaking, explaining file I/O
requires using two concepts--structures and pointers--that we will
not talk about until much later in the course. Fortunately, if you'll
just trust me a little we can ignore most of the details of these
concepts in explaining files.
By default, programs that write with ``printf'' are writing to the
display, and programs that read with ``scanf'' are reading from the
keyboard. On most operating systems, however, it is possible to set
things up so that when a program is run ``printf'' and ``scanf'' write
to and read from files instead. (This is called redirecting input and
output.) While redirection is discussed in section 1.9, it is highly
operating system dependent so we won't say much more about it.
Redirection is a powerful idea that works perfectly if you want to
read from exactly one file and write to exactly one file. If you want
to deal with multiple files, you must know how to open, read, write,
and close files explicitly from within a C program. That is what we
will study in this lesson.
Hamlet Project
hamlet@cs.utah.edu