CS Behind the Curtain (CS195 2003S)

Files in C

Summary: This laboratory provides experience retrieving data from text files using C.

Citation: This laboratory was copied (nearly verbatim) from a laboratory by Henry Walker.

Useful Files:

Contents:

Background

Text Data File

The file cities.dat contains several items of information about large American cities. More specifically, each entry consists of the name of the city (line 1), the county or counties (line 2) and the state (line 3) in which it is situated, the year in which it was incorporated (line 4), its population as determined by the census of 1980 (line 5), its area in square kilometers (line 6), an estimate of the number of telephones in the city (line 7), and the number of radio stations (line 8) and television stations (line 9) serving the city. Thus a typical entry reads as follows:

Albuquerque
Bernalillo
New Mexico
1891
331767
247
323935
14
5

A blank line follows each entry, including the last.

A Processing Program

Rather than leaving you to do everything from scratch, we have prepared citystats.c, a simple C program that lets you analyze city data.

Exercises

Exercise 0: Preparation

a. Create a new directory for this lab.

b. Make a copy of cities.dat.

c. Display it on your workstation (using less or other similar command).

d. Observe that you can edit cities.dat by using an editor, such as xemacs, vi, or gedit.

e. Make a copy of citystats.c.

Exercise 1: Code Reading

a. Review the source code for citystats.c. Take notes on the purpose of the various procedures, any data types defined, anything else interesting you note about the code.

b. Change anything you deem appropriate to make it work with your copy of the data and in your directory.

Exercise 2: Basic Statistics

a. What do you expect a typical interaction with citystats to be like?

b. Compile and run citystats to verify your answer.

Exercise 3: The Final Entry

In my experience, if you display all the data in the file, the last entry appears twice (with some awkward variation). Fix the code so that the problem doesn't happen.

Exercise 4: Code Reading, Revised

Reread the source code for citystats.c to identify answers to the following questions.

a. How does the program know which file to read?

b. How is each line of text read?

c. What does procedure readLine do?

d. Why is readLine used in readCity for city.nme, while fscanf is used for city.yar?

e. Why do you think readLine is defined?

Exercise 5: Reading User Input

How might this program differ if data were being read from the user rather than from a file?

Exercise 6: readLine

Why might someone complain about the algorithm used for readLine?

Exercise 7: Old Cities

a. Fill in the details for procedure incorporated, which will scan the entire text file and print out the data on each city that was incorporated before 1843.

b. Update incorporated so that it takes takes a year as a parameter. Update the user interface so that it asks for that number.

Exercise 8: Dense Cities

The population density of a city is the ratio of population to area. Fill in the details of populationDensity to print out the the city with highest population density.

Exercise 9: The Media

a. Fill in the details of media so that it determines which city has the lowest per capita radio and television stations.

b. Write a variant of media that takes a maximum per-capita media rate and prints all cities that have a per-capita rate less than the given amount.

c. Expand our variant so that it also takes a filename as a parameter and writes information about the matching cities to that file.

For Those With Extra Time

Allow citystats to take the name of the cities file and the action to perform as command-line arguments.

 

History

Thursday, 27 September 2001 [Henry Walker]

Monday, 19 November 2001 [Henry Walker]

Tuesday, 4 February 2003 [Samuel A. Rebelsky]

Wednesday, 5 February 2003 [Samuel A. Rebelsky]

Friday, 7 February 2003 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Fri May 2 14:19:45 2003.
The source to the document was last modified on Fri Mar 7 02:59:49 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS195/2003S/Labs/files.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu