C File Input and Output

All of the programs that we have studied to this point have done their reading from the keyboard and their writing to the display. This is just what we want if we wish our programs to be completely interactive, but there are circumstances when it is desirable to read input from or write results to a file. What are some of these circumstances?

Click here for the answer

Reading from or writing to a file is (almost) exactly like reading from the keyboard or writing to the display. If you initially create a program that does terminal input and/or output, it is extremely easy to modify it so that it does file input and/or output. We will study file I/O by beginning with a simple program that communicates with the terminal and then modifying it into an equally simple program that communicates with a file.

In fact, file I/O is so similar to terminal I/O that we recommend the following approach to developing programs that do file I/O. First, write the program so that it does all input from the keyboard and all output to the display. Then modify it, as we will show you below, so that it does input and/or output to a file as you desire.

Click on the button below to copy today's example files into your own examples directory (i.e., the directory named examples inside your home directory.)

EXECUTE Click to copy the example files.


Joseph L. Zachary
Hamlet Project
Department of Computer Science
University of Utah