CSC 161 Grinnell College Spring, 2010
 
Imperative Problem Solving and Data Structures
 

Files in C

Goals

This laboratory provides experience with reading and writing data to text files.

Prerequisites

I/O functions and strings in C

Index

Part A: Some Familiar Shell Programs

  1. Write a small C program called mycat.c that reads a text file and prints its contents to stdout. You will likely find the C library functions fgets and fputs (or puts) useful. Recall that, in contrast with scanf, fgets returns NULL when it encounters the end of the file. For this initial exercise, you may hard code the name of some file that already exists in your account as the input file. You may assume that no line in the input file will contain more than 256 characters. Don't forget to close the file when you are done!

    The output from your program should be similar to what you would get by typing "cat infile" at the shell prompt, assuming a file named infile exists in your directory.

  2. Modify your program so that it reads the name of the file from the keyboard. The program should give a useful error message if the specified file does not exist.

    Hint: Use the online manual to determine how fopen works if the specified file is not present. Recall that you can type man fopen to reference the online manual for this function.

  3. Write a small C program called mycp.c that reads two strings: a "source" file, and a "destination" file. Your program should read the source file and write its contents to the destination file. Again, your program should provide meaningful error messages in extenuating circumstances.

  4. Write a small C program called mygrep that reads two strings. The first should be a string to search for, and the second should be a filename to search in. Your program should read the file, search for the target string in each input line, and then copy just those lines which contain the target to stdout.

    You may find the C library function strstr useful for this exercise. It is declared in string.h, and information about it can be found in the man pages: "man strstr".

Part B: Processing Data Files: Median Family Income

File ~walker/c/files/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).


                         Median Income for 4-Person Families, by State, According to the U.S. Census Bureau
                        Reported November 3, 1999 on Web site http://www.census.gov/hhes/income/4person.html

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 ONE of the following questions (one of step 5, 6, or 7).
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.

Work to turn in:

Your programming for steps 5, 6, and 7 may be done in a single program or in three separate programs.


This document is available on the World Wide Web as

     http://www.cs.grinnell.edu/~walker/courses/161.sp09/lab-files.html

Part A: written 19 March 2007 by Marge Coahran
Part A: revised 18 March 2010 by Henry M. Walker
Part B: created and revised 27 September 2001 - 2 May 2005 by Henry M. Walker
Integrated Lab: revised 19 March 2007 by Marge Coahran
Integrated Lab: revised 24 January 2009 by Henry M. Walker
Integrated Lab: last revised 19 March 2010 by Henry M. Walker
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.