CSC161 2010F Imperative Problem Solving

Laboratory: File Processing

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

Prerequisites: Familiarity with Strings in C. Familiarity with basic I/O operations in C.



Exercise 1: Small Cats

Write a small C program called kitten.c that reads a text file and prints its contents to stdout. You should implement kitten using fgetc and fputc (or just putc). You should take the name of the file from the command line.

The output from kitten file should be the same as the output from cat file, provided file exists.

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!

Exercise 2: Other Small Cats

Write a small C program called kitty.c that has the same behavior as kitten.c but that uses fgets and fputs (or puts) rather than fgetc and fputc.

Exercise 3: Fixing Small Cats

Determine what changes do you need to make to kitten.c and kitty.c to support longer lines?

You may want to discuss those changes with your class mentor or teacher.

Exercise 4: Choosing Cats

Which of kitten.c and cat.c do you prefer? Why?

Exercise 5: Crazy Cats

Write a program, kat, that repeatedly reads the name of a file from standard input and then prints the contents of that file to standard output. (That is, the user may enter multiple file names and each file gets concatenated to output.)

Exercise 6: Copy Cats

Write a small C program called cpy.c that reads two file names from the command line, a source and a destination. Your program should then read the source file and copy its contents to the destination file.

Exercise 7: Curious Cats

Write a small C program called cktc.c that reads two strings from the command line, a string to search for and 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>.

Exercise 8: Checking Cats

Experience suggests that most students will not put error checking in the previous programs. Please add checking so that the programs report appropriate error messages, such as when asked to read a file that does not exist.

Exercise 9: Testing Cats

With a classmate, determine how you might implement unit tests for the procedures we just wrote. You need not implement those unit tests, but you should be prepared to describe your strategy to the class.



19 March 2007 [Marge Coahran]

  • Wrote the shell command problems.

27 September 2001 - 2 May 2005 [Henry M. Walker]

  • Created and revised the state processing problems.

19 March 2007 [Marge Coahran]

  • Integrated two parts.
  • Other revisions.

24 January 2009 [Henry M. Walker]

18 March 2010 [Henry M. Walker]

  • Revisions to shell command problems

19 March 2010 [Henry M. Walker]

  • Further revisions.
  • This version available at .

Thursday, 11 November 2010 [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 Thu Nov 11 20:19:49 2010.
The source to the document was last modified on Thu Nov 11 20:18:56 2010.
This document may be found at
A PDF version of this document may be found at

Samuel A. Rebelsky,

Copyright © 2010 Marge Coahran and Samuel A. Rebelsky. Please contact us for permission to reuse materials.