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

Laboratory Exercise on FILEs

Goals:

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

In the terminal, look up the man page for gets and getc.

  1. Look at the parameters for gets and getc. What are the differences?

  2. Read the section on the man page titled "BUGS". Are there any dangers with either gets or getc?

    * Notice that getc is similar to getchar in that both take a single character, which can be a letter, number, punctuation mark, or blank space.

What is printf?

  1. Although the 'function' printf is the most common print function in C, there are several related functions for output, including the function fprintf. The main difference is that fprintf permits the user to determine the specified output stream, while printf uses the default output stream stdout. IN FACT, printf is actually just a macro that expands to fprintf(stdout, "YOUR STRING HERE") So the following two lines of code:

    printf ("This is output.\n");
    fprintf (stdout, "This is output.\n");

    print the same output in the terminal. The output stream stderr also prints output in the terminal, since error messages are, in the local system, sent to the terminal by default.

  2. Explain why some programmers might use fprintf in place of printf.

Part B: 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".

  5. Sometimes data comes in a certain format. A common format is comma separated values. In a CSV file data is formatted much like a table where each new line is a row and each field (column) in a row is denoted with a comma. Consider a case where a song is written in a file in the following CSV-like format:

    song-name
    song-composer
    column-1-heading(duration),column-2-heading(frequency)
    duration1,frequency1
    duration2,frequency2
    duration3,frequency3
    ...
    ..
    .

    Write a program that reads in the song in the file spirit-song-file.csv and plays the entire song on the robot. Do not create an internal data structure to hold the song. Simply loop until there are no more lines to be read and play each note in each iteration. The program should also indicate what song is currently playing using the internal name (the first line) of the song.

    Note: strtok is an awesome function. Consider using it to parse each line (which should have been read in using fgets).

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.