See Appendix A for answers.
Write a program to read in a filename from STDIN
,
and then open that file and display its contents with each line
preceded by the filename and a colon. For example, if
fred
was read in, and the file
fred
consisted of the three lines
aaa
, bbb
, and
ccc
, you would see fred: aaa
,
fred: bbb
, and fred: ccc
.
Write a program that prompts for an input filename, an output
filename, a search pattern, and a replacement string, and then
replaces all occurrences of the search pattern with the replacement
string while copying the input file to the output file. Try it on
some files. Can you overwrite an existing file? (Don’t try it
with anything important!) Can you use regular expression characters
in the search string? Can you use $1
in the
replacement string?
Write a program to read in a list of filenames and then display which of the files are readable, writable, and nonexistent. (You can perform each test for each filename as you read them, or you can perform the tests on the entire set of filenames after you’ve read them all. Don’t forget to remove the newline at the end of each filename that you have read.)
Write a program to read in a list of filenames and then find the oldest file among them. Print out the name of the file and the age of that file in days.