Whenever I enter a text file for my program to read I always get the message:
Unable to open file: big_file.txt
Ending program.
I am not sure what part of the program is not working. I assume that it is either something in main() or validate_input() functions:
FILE* validate_input(int argc, char* argv[]){
FILE* fp = NULL;
if(argc < 2){
printf("Not enough arguments entered.\nEnding program.\n");
exit(0);
}
else if(argc > 2){
printf("Too many arguments entered.\nEnding program.\n");
exit(0);
}
fp = fopen(argv[1], "r");
if(fp == NULL){
printf("Unable to open file: %s\nEnding program.\n", argv[1]);
exit(0);
}
return fp;
}
int main(int argc, char* argv[]){
char** lines = NULL;
int num_lines = 0;
FILE* fp = validate_input(argc, argv);
read_lines(fp, &lines, &num_lines);
print_lines(lines, num_lines);
free_lines(lines, num_lines);
fclose(fp);
return 0;
}
I expect the program to output each line of a given text with a number in front counting each line, but every time I input a file it claims it cannot be read.
UPDATE:
When I add fprintf(stderr, "Unable to open file %s: %s\n", argv[1], strerror(errno));
my output is:
Unable to open file big_file.txt: No such file or directory
but the file is on my desktop.
Not sure if this is the right place for the file but here it is: