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

Laboratory Exercise on FILEs


The goal of this lab is to introduce basic character, and string, input and output using files.

Part A: Revisiting Methods of Character and String Input

gets vs getc

  1. Using getc write a simple program that gets the specified character of the terminal input "I love C!".
  2. Modify the program above to loop through and print the whole input that you enter.
  3. Now modify the program to print the whole input using gets and does not have loops.

What is printf?

  1. Explain the difference between printf and fprintf.
  2. Which one would you use to write to a file?
  3. How would you use fprintf to write to standard output?
  4. Which is the easiest way to write to standard output?

Part B: What You Can Do With Files

Some useful common utilities

  1. Write a small program that counts all the characters in a file. Your output should give you how many letters, spaces, newlines, and other characters there are.

  2. Write a program that will count how many words there are in your file.


  1. Bioinformatics solves problems in biology with the use of computer science.

    The file mouse_gene has genetic data from a mouse. This file contains information about four different genetic regions, each beginning with a tag, such as > AB000913 1. Unfortunately, having all these regions in the same file is not particularly helpful, and it would be useful to divide the initial file into four smaller and distinct files, one for each genetic region.

    Write a C program that will take in this file as an input and then produce 4 files as an output. Each of the output files should contain a tag such as > AB000913 1 that indicates different smaller genetic regions, and the genetic regions to which the tag corresponds.

A familiar Shell Program: cat

  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.

Feedback Welcome

Development of laboratory exercises is an interative process. Prof. Walker welcomes your feedback! Feel free to talk to him during class or stop by his office.