CSC 161 Grinnell College Fall, 2011
Imperative Problem Solving and Data Structures

Second Laboratory Exercise on FILEs


The goal of this lab is to cover more complex access to and manipulation of FILEs in C.

Part A: Processing Data Files: Median Family Income

File state-income.dat contains information about the median annual income for a 4-person family for the various states for the years 1997 back to 1979. As shown in the file sample that follows, the first five lines contain header information (2 lines of title, a blank line, column headings for various years, and another blank line). Thereafter, the information about each state is on a separate line. Within a line, the state name is left justified in the first 21 characters, and income figures are in 6-character-wide columns (the income appears as 5 characters, and a blank spaces separates one year's income figure from the next).

To illustrate the format, the first part of the file is shown below.

                         Median Income for 4-Person Families, by State, According to the U.S. Census Bureau
                        Reported November 3, 1999 on Web site

Year                  1997  1996  1995  1994  1993  1992  1991  1990  1989  1988  1987  1986  1985  1984  1983  1982  1981  1980  1979

United States        53350 51518 49687 47012 45161 44615 43056 41451 40763 39051 36812 34716 32777 31097 29184 27619 26274 24332 22395
Alabama              48240 44879 42617 41730 37975 39659 37638 35937 34930 33022 31221 29799 28407 26595 25117 24181 22443 22026 18613
Alaska               57474 62078 56045 53555 51181 49632 49721 51538 48411 47247 47106 41292 42897 44017 38238 31823 35834 32745 31037
Arizona              47133 45032 44526 41599 39679 39900 39364 38799 38347 36892 35711 33477 32129 29431 27551 29835 25163 23832 23000
Arkansas             38646 36828 38520 36510 32594 36682 34566 31913 31853 28665 27415 27157 26255 23075 21524 20710 20583 19448 18493
California           55217 53807 51519 48755 44643 46774 46643 45184 42813 41425 40218 37655 36223 33711 31967 29885 27763 26070 25109
Colorado             58988 53632 50941 48801 47112 45021 43136 41803 40265 39095 37778 36026 35214 34154 32294 30663 28756 25943 25228
Connecticut          72706 67380 62157 62107 59288 55061 54479 53931 53313 50720 47195 44330 40677 39070 37703 35361 31108 28376 24410

Use the information in this file for the following questions.

Do TWO of the following questions (one of step 1, 2, or 3).
Additional problems may be done for further credit.

  1. Extract from this file the name of each state and its median income for 1995. Put the results in a new file, called state-income-for-1995

    That is, after processing the above file, the new file state-income-for-1995 should begin:

    State          1995 Median Income
    United States        49687
    Alabama              42617
    Alaska               56045
    Arizona              44526
    Arkansas             38520
    California           51519
  2. From the original state-income file, extract each state's median income for a given year.

    That is, the program (or program segment) should ask a user for a year between 1979 and 1997 (inclusive) and print to the screen a listing of state names and their median incomes for that year.

  3. From the original state-income file, print to the screen the names of those states for which the median income decreased from some year to the next.

For Your Utmost Pleasure..

  1. Watch the center of population move as the US grows:

  2. Increase the animation's size for even more viewing pleasure.

    Hint: Use the VIEW FULL SCREEN feature.

Part B: Binary File Input and Output

For those with extra time

Previously, you have only worked with writing character data to files in the form of chars and strings. However, complex programs often require you to write large chunks of information to a file. Examples include large databases, a scientific dataset, or even an executable program itself. In C there are two functions that facilitate this this type of reading and writing: fread and fwrite.

  1. Review the man pages for fread and fwrite. How are these functions different from fprintf and fgets? How are they similar?

Writing Arbitrary Data

fwrite literally writes bits (the 0s and 1s that represent whatever is pointed to in its first argument) to the file you specify in its last argument. However, to know how many bits to write you have to tell fwrite how big (how many bytes) the data you want to write is and how much data there is. This information corresponds to the second and third arguments respectively.

Suppose you want to write an array of 10 doubles (we'll call it array) to a file. The call to fwrite would look something like this:

fwrite (array, sizeof(double), 10, yourFile);

If you wanted to write just one Pixel structure you had previously declared as pxl, the call would resemble:

fwrite (&pxl, sizeof(Pixel), 1, yourFile);

Do either 7 or 8:

  1. Write a program that reads in a series of integers from standard input using any method you prefer. The program should then imediately write these integers to a binary file of your choosing using fwrite.

  2. Write a program that instructs the robot to take a picture and then converts that picture to a Pixel array. Code for the conversion from Picture to Pixel array can be found in the module on structs. Write the width as an integer to a binary file. Then write the height to the file. Finally, write the entire Pixel array to the file using fwrite.

Reading Bits Back In

Not suprisingly, fread functions exactly like fwrite except in reverse. It reads the data in in the same manner -- where you specify the data to be read, the sizeof the data, and how many 'packets' of data you want.

Complete the question you chose to do earlier:

  1. Write a program that reads in integers from the binary file and writes them - each on a new line - to stdout.

  2. Write a program that reads in an array of Pixels from a file and displays the picture using rShowPicture from the MyroC header. You should first read in the width and height of the picture (the first 8 bytes [two four byte integers] of the file) so you know how many Pixels are stored in the picture. Then you can read the entire array back in using fread.

    Note you will have to create a blank picture first and then set each pixel according to the data obtained from fread.

Reminder: Complete Evaluation Form

When you have finished this lab, be sure to fill out its evaluation form in the "Lab Evaluation" section for CSC 161 on Pioneer Web.