Syntactically you don't have any function call in that. What is the correct syntax to call a function that does not take parameters? Besides, dhayden did already explain why you should not hardcode "comp201_head" into the usage string; the user can rename the program file.
I don't think that the argc can ever be 0. What value there should be in argv[0]?
There are several situations where you should print the usage instruction. That is why you have created a separate, easy to call function.
You say "problems", but don't explain what they are. The better you are at explaining a problem, the more likely you are to spot the reason for it yourself.
Lines 35-46 almost repeat lines 17-27. Make a separate function for actually printing one file and call that function. That way you don't have to write same code multiple times. (Just like with the usage function.)
Line ensures that argc is 3 or greater, but line 14 creates indices that will be "out of bounds" for the argv array of words.
Line 31 "else if true" is true every single time.
You wrote:
If your program is called with invalid inputs, it should output the usage statement above and exit. Invalid inputs include
(1) no file name,
(2) an argument that starts with a dash (-) but is not the only supported switch
(-n), or
(3) not having an integer immediately following the supported switch.
You don't have a default value for numLines. I didn't see one in the original spec so you may be able to make up something for yourself, but there should be some default.
You don't check for "-n" anywhere.
In pseudo-code, you want to do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
set currentArg = 1;
if (currentArg <= argc) usage();
if (argv[currentArg][0] == '-') {
if (argv[currentArg][1] == 'n') {
if (++currentArg <= argc) usage();
numLines = atoi(argv[currentArg++]);
} else {
usage(); // "-" option but it isn't -n
}
}
// currentArg now points to the first file arg
for (; currentArg < argc; ++currentArg) {
// print the first numLines of argv[currentArg]
}
Sorry, but the point of my previous post was that you need to restructure your code. For example, right now if you enter "theProg file1 90125.fanz file3" it will think that 91025 is the argument to -n (at line 11).
You need to initialize numLines to something.
I suggest that you follow the pseudo-code that I gave in my last post.
This means that if no -n switch is provided, output all lines of the file; if it is provided, output up to that
number,
It is kind of hidden there, but you have a clear (although tricky) default that you should use. I would use 0.
Why? Lets suppose that you write and use a function:
1 2 3 4 5
void showlines( constchar * name, size_t limit );
// param name is used as filename
// param limit:
// if ==0 show all lines
// if >0, show at most limit lines