CSC161 2011S Imperative Problem Solving
[Skip to Body]
Primary:
[Front Door]
[Schedule]
[Piazzza]
-
[Academic Honesty]
[Instructions]
Current:
[Current Outline]
[Current EBoard]
-
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[Documents]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
[CSC161 2010F (Rebelsky)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
Summary: This laboratory provides experience with reading data from and writing data to text files.
Prerequisites: Familiarity with Strings in C. Familiarity with basic I/O operations in C.
Write a small C program called kitten.c that reads a
text file and prints its contents to stdout. You should
implement kitten using fgetc and
fputc (or just putc). You should take the
name of the file from the command line.
The output from kitten file should be the same
as the output from cat file, provided
file exists.
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!
Write a small C program called kitty.c that has
the same behavior as kitten.c but that uses
fgets and fputs (or puts)
rather than fgetc and fputc.
Determine what changes do you need to make to kitten.c and
kitty.c to support longer lines?
You may want to discuss those changes with your class mentor or teacher.
Which of kitten.c and cat.c do you prefer? Why?
Write a program, kat, that repeatedly reads the name
of a file from standard input and then prints the contents of that
file to standard output. (That is, the user may enter multiple
file names and each file gets concatenated to output.)
Write a small C program called cpy.c that reads two
file names from the command line, a source and a
destination. Your program should then read the source
file and copy its contents to the destination file.
Write a small C program called cktc.c that reads two strings
from the command line, a string to search for and 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>.
Experience suggests that most students will not put error checking in the previous programs. Please add checking so that the programs report appropriate error messages, such as when asked to read a file that does not exist.
With a classmate, determine how you might implement unit tests for the procedures we just wrote. You need not implement those unit tests, but you should be prepared to describe your strategy to the class.
19 March 2007 [Marge Coahran]
27 September 2001 - 2 May 2005 [Henry M. Walker]
19 March 2007 [Marge Coahran]
24 January 2009 [Henry M. Walker]
http://www.cs.grinnell.edu/~walker/courses/161.sp09/labs/lab-files.shtml.
18 March 2010 [Henry M. Walker]
19 March 2010 [Henry M. Walker]
Thursday, 11 November 2010 [Samuel A. Rebelsky]
f{put,get}c
and f{get,put}s.
http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2010F/Labs/files-lab.html
Tuesday, 18 April 2011 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2010F/Labs/files-lab.html
[Skip to Body]
Primary:
[Front Door]
[Schedule]
[Piazzza]
-
[Academic Honesty]
[Instructions]
Current:
[Current Outline]
[Current EBoard]
-
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[Documents]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
[CSC161 2010F (Rebelsky)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Mon Apr 18 22:04:39 2011.
The source to the document was last modified on Mon Apr 18 22:04:37 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Labs/files-lab.html.
A PDF version of this document may be found at
http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Labs/files-lab.pdf