File Input and Output
In this lab you will gain experience with both basic file input and output involving numbers, characters and strings. File readfiles.c has some examples for how to handle opening a file for reading, reading until the end of the file and closing files properly.
Catching File Errors
-
Download, review, and compile the
program
fopen-test.cfrom the reading.- Run the program, trying to generate as many different error messages as you can.
-
Change the
fprintfcall within the(stream == NULL)test to use a call toperrorinstead. Recompile and run the program to generate the associated error. How does the output change? -
The reading advised you that the
error-reporting
fprintfwithin theargctest should not be changed to aperror. 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
-
Like the command line utility,
wc, write a small program calledmywc.cusinggetcto count all the characters in a file whose name is given as a command line argument. The program simply prints 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 from the reading, be sure you cite it appropriately.)
-
In order to to test for
EOFcorrectly, the textbooks and manual pages remind us thatgetcreturns anint, not achar -
To distinguish between the end of file condition and a read
error, your program should use the function
ferroraftergetcreturnsEOF - If a read error occurred, your program should report a human-readable message and exit.
- Remember to close the file when you are done reading from it.
-
Augment your
mywc.cprogram 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
isspacefrom<ctype.h>should prove helpful.
-
Augment your
mywc.cprogram so that it will also count and print how many lines there are in the specified file.
Concatenating files: cat
-
Write a small C program called
mycat.cthat reads all the text files whose names are given on the command line and prints their contents tostdout.-
You will likely find the C library functions
fgetsandfputs(orputs) useful. Recall that, in contrast withscanf,fgetsreturnsNULLwhen 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).
-
You will likely find the C library functions
Handling numeric file I/O
- This program will read a series of integers from a file, one at a time. Your program will convert each number from quarts to liters and save the resulting number of liters to a new file. If the output file exists, your program should write over it.
- Use the fscanf function to read each number until EOF is reached
- You will need to create two FILE * streams, one for input and one for output.
- You should not try to save an array of numbers. Just read a number, convert it to liters, and print the result to the output file
- You may ask the user for the names of the input and output files, using the usual scanf function