int main(int argc, char **argv){
// If the user does not have the right amount of arguments exit the program and display message
if(!(argc >= 2)){
printf("Usage: %s -l -c -w -L <filenames>");
exit(-1);
}
size_t iscFlag = 0;
size_t islFlag = 0;
size_t iswFlag = 0;
size_t isLFlag = 0;
FILE *src;
puts("Work work");
// For loop that checks for all the flags
int i;
for(i = 1; i < 5; i++){
if(strcmp(argv[i], "-c") == 0){
iscFlag = 1;
}else if(strcmp(argv[i], "-l") == 0){
islFlag = 1;
}else if(strcmp(argv[i], "-w") == 0){
iswFlag = 1;
}else if(strcmp(argv[i], "-L") == 0){
isLFlag = 1;
}
}
I'm trying to make a simple for loop that checks for flags to recreate a wc command in a UNIX type terminal. Anyone know what's up with the for loop? I get a segmentation fault on it. There is more code but I'm just posting a part of it.
Thanks in advance.
With
for(i = 1; i < 5; i++){
if(strcmp(argv[i], "-c") == 0){
you should ensure that argc >= 5
, or the strcmp
will receive a NULL
pointer when i == argc
.