Lab03 CISC105, Fall 2004, P. Conrad

Goals:

You will write a C program that reads input from a file on the disk (not from the keyboard!) and writes its output to a file on the disk (not to the screen!). You'll also continue working with functions, variables, input and output.

Preliminary Step

Under your cisc105 subdirectory, make a new directory for lab03, just like we did for lab02 last week (see last week's lab if you are not sure what to do).

C programming with external input and output files

Complete exercise 3.6 from your Tan and D'Orazio textbook, p. 163. Call your program file lab03a.c.

First, it is recommended that you sketch out your program on paper. When you are ready to type it in, cd into your ~/cisc105/lab03 directory, and use the text editor to enter your program.

Note that this program does not use loops or arrays (those are concepts covered in later chapters). Each of the values a1, a2, etc, from lines 2 through 6 of the input file should be stored in its own variable. You may reuse the same variables a1, a2, etc. for the second occurence of these numbers on lines 8 through 12.

To test your program you will need to use the text editor to create a data file called DISTANCE.DAT. That file should just contain numbers, in the format shown on page 164 of your text. You don't have to do anything special to this file other than create it with the text editor (for example, you don't have to compile it.) When you run your program, the program will open this file up and read data from it. Lesson 3.6 (starting on p. 120 of your text) explains how this works. Be sure that you use all capital letters for the name of this file, both when you create it with the editor, and when you put the file name in your program code.

The file DISTANCE.OUT is one you will not create with the editor. Instead, that file should appear in your directory when you run your executable program. Lesson 3.8 in your textbook (starting on p. 132) explains this in more detail.

To get full credit, be sure you look over the reading notes for chapter 3 (around Lesson 3.6 and Lesson 3.8) about using an if statement to check the result of opening a file for input or output.

Finishing up (and grading)

Make a script called lab03.txt. In this script, do the following steps. If you are unsure what to do, consult last week's lab for hints.

  1. Enter a command to change your working directory to your lab03 subdirectory.
  2. Enter a Unix command to print your working directory.
  3. Enter a Unix command that will list out your files.
  4. If step 3 showed a file called DISTANCE.OUT, enter a unix command to delete this file, and then list out your files again. It is important that DISTANCE.OUT doesn't exist before we start running the program.
  5. Enter a command to show the contents of the file lab03a.c
  6. Enter a command to show the contents of the file DISTANCE.DAT
  7. Enter a command to compile your lab03a.c file.
  8. Enter a command to run your program.
  9. Repeat the command from step 3 to list your files. You should now have a DISTANCE.OUT file.
  10. Enter a command to list out the contents of DISTANCE.OUT.
  11. End your script.

When you are done creating your script, print it (see previous labs for the print command), and submit lab03.txt and lab03a.c to WebCT.

Grading

Total: 100 pts

*** end of lab03 ***