I'm trying to write a code for a class that takes a filename and target character from command line and counts the number of times the target character appears in the file. Every time I run the program, it gives returns half the number of characters that are actually are. Why is this happening?
[code]
#include <stdio.h>
#include <string.h>
int char_count(const char *filename, char target_char);
int main(int argc, char **argv){
if(argc != 3){
printf("./file_char_count <filename> <target character>\n");
return 1;
}
if(strlen(argv[2]) > 1){
printf("Argument should only contain one character\n");
return 2;
}
if(char_count(argv[1], *argv[2]) == -1){
printf("The file does not exist\n");
return 3;
}
else{
printf("%i\n", char_count(argv[1], *argv[2]));
}
return 0;
}