As Jens Gustedt said: remove the colons from the options to make them not take an argument. Then, when option-processing is done (i.e. getopt
returned -1
), check if optind
is less than argc
meaning there was an extra argument (your file-name). Like this:
/* after the while-loop */
if(optind < argc){
fileName = argv[optind];
optind++;
}else{
perror("No file specified");
return -1;
}
When getopt
is done optind
points to the first entry in argv
that wasn't processed by getopt
.