First of all, your use does not match the validation of your argument. According to usage, you should use one of:
head <filename>
head <filename> -n <count>
In other words, is argv[1]always the name of the file, argv[2]- this is the one that should be set to -n, if there are more than two arguments.
-, VLA ( ), , , - :
char *fileName = argv[1];
(, , fopen, ), , .
, if or, and. , argc 2, 4, .
- :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int usage (void) {
printf ("Usage: head <file>\n");
printf (" or: head <file> -n <number of characters>\n");
return -1;
}
int main (int argc,char *argv[]) {
char *fileName;
int lineCount;
if ((argc != 2) && (argc != 4)) return usage();
if ((argc == 4) && (strcmp(argv[2], "-n" != 0)) return usage();
fileName = argv[1];
lineCount = (argc == 2) ? 10 : atoi (argv[3]);
if (linecount < 0) lineCount = 0;
}