For this assignment you will write a program that reads integers from an input file, and writes the sum of the integers to an output file. The name of the input file and the name of the output file will be provided as command line arguments, with the input file first and the output file second.
Use fscanf()
and fprintf()
for input and output. See the example code for guidance.
If your program runs without errors, there should be nothing printed to standard output, since the output of the program is being written to a file.
You can create a file for input in Visual Studio Code by clicking File -> New File
and then saving it in the cloned assignment directory.
For example, say you create an input file named input.txt
and input.txt
contains the following:
1 2 3
4 5 6 7
Then you run your program like this:
./filesum input.txt output.txt
No output should be printed, but output.txt
should contain 28. You can view the output file by opening it in Visual Studio Code or by printing the contents in the terminal using cat
:
cat output.txt
If there are errors, your code must print an error message and return a specific exit code depending on the error. Below are the different error conditions that your program should deal with, and the exit code that should result under each condition.
You can force an error in opening the input file by providing the name of a file that does not exist. You can force an error in the output file by giving an absolute path of a file that would be written into a directory that does not exist. For example:
./filesum does_not_exist.txt output.txt
Error opening does_not_exist.txt for reading
./file_infilesumteger_sum input.txt /directory/that/does/not/exist.txt
Error opening /directory/that/does/not/exist.txt for writing
./filesum
Usage: ./filesum <input file> <output file>
Many submissions to past assignments have hardcoded the name of the executable in the usage message. It is better to let printf()
insert the name of the executable for you, so that the usage message is correct no matter what it is named (it could be a.out
or some other name). You can do that like this, since argv[0]
is the name of the executable:
printf("Usage: %s <input file> <output file>\n", argv[0]);
You can run the GitHub testing locally with the command:
make gh-test-filesum
Push your code to GitHub. Your code will be automatically tested for correctness.
Your grade for this assignment will be out out of 10 points: