| CSC 161 | Grinnell College | Spring, 2015 |
| Imperative Problem Solving and Data Structures | ||
The goal of this lab is to introduce basic character, and string, input and output using files.
Using getc write a simple program that gets the first character of the terminal input "I love C!".
Modify the program above to loop through and print the whole input that you enter.
Now modify the program to print the whole input using gets and does not have loops.
Explain the difference between printf and fprintf.
Which one would you use to write to a file?
How would you use fprintf to write to standard output?
Which is the easiest way to write to standard output?
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.
Write a program that will count how many words there are in your file.
In this program, consider a word to be any consecutive sequence of
letters. Thus, you can count a word by identifing an initial letter and
then skipping through any subsequenct letters until you read a non-letter.
(In this approach hyphenated words (e.g., problem-solving) and contractions
(e.g., don't) will count as two words.)
Bioinformatics solves problems in biology with the use of computer science.
The file worm)gene.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.
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.
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.
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.
Development of laboratory exercises is an iterative process. Prof. Walker welcomes your feedback! Feel free to talk to him during class or stop by his office.