CSC 161 Schedule Readings Labs & Projects Homework Deadlines Resources

Basic File Input and Output

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

File Utilities

Catching File Errors

  1. Download, review, and compile the program fopen-test.c from the reading.
    1. Run the program, trying to generate as many different error messages as you can.
    2. Change the fprintf call within the (fp == NULL) test to use a call to perror instead. Recompile and run the program to generate the associated error. How does the output change?
    3. The reading advised you that the error-reporting fprintf within the argc test should not be changed to a perror. Make that change anyhow, then recompile and run the program to generate the associated error. What output do you get? Explain why that output is sensible in this context.

Counting characters, words, and lines: wc

  1. Like the command line utility, wc, write a small program called mywc.c using getc to count all the characters in a file given as a command line argument. The program simply print how many letters, spaces, newlines, and other characters there are in the file.
    • Your program should report human readable errors and exit if the file cannot be opened for reading. (If you use example code, be sure you cite it appropriately.)
    • In order to to test for EOF correctly, the textbooks and manual pages remind us that getc returns an int, not a char
    • To distinguish between the end of file condition and a read error, your program should use the function ferror after getc returns EOF
    • If a read error occurred, your program should report a human readable message and exit.
    • Remember to close the file when you are done.
  2. Augment your mywc.c program so that it will also count and print how many words there are in the specified file.
    • In this program, consider a word to be any consecutive sequence of letters separated by white space. Thus, you can count a word by identifing an initial letter and then skipping through any subsequent letters until you read a whitespace character. You'd start a new word again once you read a non-space character.
    • The function isspace from <ctype.h> should prove helpful.
  3. Augment your mywc.c program so that it will also count and print how many lines there are in the specified file.

Concatenating files: cat

  1. Write a small C program called mycat.c that reads all the text files given on the command line and prints their 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 or an error.
    • If any file cannot be opened for reading, your program should report human readable error and continue with the next file. (If you use example code, be sure you cite it appropriately.)
    • If a read error occurred, your program should report a human readable message to the standard error stream and continue with the next file.
    • Remember to close each file when you are done (even if there was a read error).

Bioinformatics

Bioinformatics solves problems in biology with the use of computer science. Here is a simple example regarding some textual data that might be used.

The file nucleotides.txt has genetic data (Source: National Center for Biotechnology Information) from C. elegans, a roundworm often used as a model organism for the study of biological systems. 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.

  1. Write a C program that will take reads this file as an input and then produce four separate files as an output.
    • The name of each file should be the the tag base plus a .txt extension (i.e., AB000913.txt).
    • Each of the output files should contain the tag (i.e., >AB000913 1) that indicates different smaller genetic regions, and the genetic regions to which the tag corresponds.