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.
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.
Rather than leaving you to do everything from scratch, we have prepared
simple C program that lets you analyze city data.
a. Create a new directory for this lab.
b. Make a copy of
c. Display it on your workstation (using
less or other
d. Observe that you can edit
cities.dat by using an editor,
such as xemacs, vi, or gedit.
e. Make a copy of
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.
a. What do you expect a typical interaction with
to be like?
b. Compile and run
citystats to verify your answer.
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.
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
d. Why is
readLine used in
fscanf is used for
e. Why do you think
readLine is defined?
How might this program differ if data were being read from the user rather than from a file?
Why might someone complain about the algorithm used for
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.
incorporated so that it takes takes a year as
a parameter. Update the user interface so that it asks for that number.
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.
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.
citystats to take the name of the cities file and the
action to perform as command-line arguments.
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]
For those with extra time
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