Laboratory Exercise on File Streams
Overview:
This lab introduces basic character, and string, input and output using files.
Work Started in Class
Part A: Revisiting Methods of Character and String Input
gets vs getc
-
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.
What is printf?
-
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?
Part B: What You Can Do With Files
Some useful common utilities
-
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.)
Homework
Bioinformatics
-
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.
A familiar Shell Program: cat
-
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.
Part A: written 19 March 2007 by Marge Coahran Part A: revised 18 March 2010 by Henry M. Walker Part B: created and revised 27 September 2001 - 2 May 2005 by Henry M. Walker Integrated Lab: revised 19 March 2007 by Marge Coahran Integrated Lab: revised 24 January 2009 by Henry M. Walker Integrated Lab: revised 16 August 2011 by David Cowden 1-10 rewritten on 10 December 2011 by Dilan Ustek (q 11-12 kept from David Cowden) reformatted 2-3 February 2014 by Henry M. Walker readings added 19 September 2014 by Henry M. Walker text revised with feedback from Jerod Weinman 17 April 2015 by Henry M. Walker reformatted and edited 25 October 2016 by Henry M. Walker |
![]() ![]() |
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |