Tasks:
Part 1
Page 568 of your textbook has a program that copies a file to a new file.
Implement it, that is, type
the program (or get it from the book's website),
compile it, and run it a few times with different input.
Make sure to retain the comments indicating where it came from.
Part 2
Make a copy of the program from part 1, calling it
lab11_part2.c or something equally good.
Alter the comments indicating where it came from to say that you
changed it.
In this part, your program should scan and process the arguments passed by the command line. That is, it should figure out what the arguments mean, indicate if there is a problem, and set values/variables to follow up. For example, in this part, your program will note if the user passes "-u" indicating that he/she wants to convert to upper-case, but the actual conversion will take place in part 3. Again, for part 2, your output should say if/what conversion will be done, but do not do the conversion in part 2.
Have it examine the arguments passed by the command line. If the user passes "-h", it should print out some help information, then exit with a code of 0. Show that this works.
One reason to use the dash as part of the argument is so that the program can tell an option from another piece of information. In this program, "-h" means help, while "h" could be a filename. Have your program expect two filenames, a file to read, and a file to write. In the event that the user passes "-h" with or without other things, your program should print the help information and quit, regardless of where on the command line it appears. For example, passing "one -h two" should cause the help information to be displayed, and the program should quit. If the user passes "file1 file2" it should open file1 for reading and file2 for writing. If the user passes "file1", the program should indicate that it expects a second filename, and exit with a non-zero code. If "file1" does not exist (or cannot be opened), your program should say this and exit with a non-zero code.
Another option to look for is "-u" for upper case, and "-l" (that's a lower-case L) for lower case. The user might pass "-u", or "-l", or neither one, but passing both should result in an error message, and the program should exit with a non-zero code.
If the user passes "-u", your program should (in part 3) convert all the alphabetic characters from the input file into upper-case characters before writing them to the output file. For example, if the input file contains "Ab 123, xYz" your program should create an output file with "AB 123, XYZ".
If the user passes "-l", your program should (in part 3) convert all the alphabetic characters from the input file into lower-case characters before writing them to the output file. For example, if the input file contains "Ab 123, xYz" your program should create an output file with "ab 123, xyz".
Have a Boolean value for help, uppercase, and lowercase. Set them all to false at first, then change them to true as appropriate when scanning the arguments passed from the command line.
Use "#define" for a DEBUG flag as well, and if true, output the status of these flags in a verbose way. That is, you could output these as "100", but that does not convey information very well. It is much better to have it output "help: true\n lowercase: false\n uppercase: false\n inputfile: 'file1.txt'\n outputfile: ''". This is much easier to follow. (In this case, the outputfile was not specified.)
Part 3
Make a copy of the program from part 2, calling it
lab11_part3.c or something equally good.
In this version, implement the upper or lower-case conversion.
This is actually very easy to do in C.
To convert lower-case to upper-case, subtract 'a' and add 'A'.
To convert upper-case to lower-case, subtract 'A' and add 'a'.
// Suppose that the character to convert is in "ch"
if ((ch >= 'a') && (ch <= 'z')) {
// It is lower-case
if (convertToUpper)
ch = ch - 'a' + 'A'; // convert to upper
} else if ((ch >= 'A') && (ch <= 'Z')) {
// It is upper-case
if (convertToLower)
ch = ch - 'A' + 'a'; // convert to lower
}
Test it out, and make sure that it works as expected.
Criteria:
The ultimate goal is to create programs that solve the problems.
Each task should have a test associated with it, to verify that it works.
When all tasks have working solutions, they should all work together to
solve the problem. Keep in mind that we will check to see what tasks
are completed, and if your program does not completely
solve the problem, it will still
receive partial credit.
When you are ready to turn this in, remember to follow the directions on turning in labs/homeworks.