np, I had to explain this to a guy today in my semester 2 class, he still has not handed in the assignment from last year and it was due friday last week. Couldn't understand the assignment because he thought simply declaring file names would be easier than passing through commandline...
My god some people need to be shot, it was not until I said to him, will your program accept my file called "yousuck.dat" as input file? he said No, so I said how is that better to declare them inside the program then ?
I think he had an lightbulb moment, only it was 2 months too late. I told him he should probably re-enroll in semester 1 again because he wasn't going to pass being that it was last year and he still hasn't finished assesment, he didn't believe me but whatevers.
Anyhoo, I think you should have a go at this, basically what the program is required to do is take any number of arguments, but should be in the format of:
1 2 3 4 5
|
program_name -i inputFile.txt -o outputFile.txt
// or
program_name -o outputFile.txt -i inputFile.txt
|
Note: the file names can be anything. Whatever the user wants to use as input and output.
your program is to parse the command line arguments and store the input file using the " -i " switch and output file using the " -o " switch.
duplicate file names are not allowed, and file names must have a length of at least 1. exit on errors.
The way I wrote mine, would take any number of arguments so if the user typed:
a.exe -i -i -i -i -i -o -i -p -o output.txt -o -o -o -j -i input.txt
it would still work as required. any invalid switches need to produce an appropriate error and exit program.
the reason the above would work is if you check every -i the following string is stored in the inputfile variable however it is overwritten by preceeding -i / -o switches.
the above is similar to this:
a.exe -i infile.txt -o outfile.txt -i foo.txt -i bar.dat -i foo.txt -i inputFile.txt -o oranges.txt -o outputFile.txt
Not to be confused with this:
a.exe -i infile.txt -p punchingbag
which should produce an error: Error switch [ -p ] unknown, usage: -i inFile -o outFile
after that the program was required to store the input file in an array, sort it and then put into output file.
However just doing the command line args was the most challenging part.
edit2: another note about that long strange argument list:
-o -j -i input.txt
"-j" would be stored as the file name for the output, however this is fine, it would not be until I opened input stored in array sorted array, and then went to open output and would get an error "Unable to open: -j, check file names and try again".