1

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: enter image description here

Christina
  • 61
  • 6
  • Try `fprintf(stderr, "Unable to open file %s: %s\n", argv[1], strerror(errno));`. What do you get? – melpomene Jun 05 '19 at 06:31
  • Are you sure the file exist? (this code is quite simple, I'm starting looking at the most simple things ;) ) And add `perror("fopen");` in the `if (fp==NULL)`, it while give you some info – Phantom Jun 05 '19 at 06:37
  • @melpomene it says "errno" is an undeclared identifier, what should it be? – Christina Jun 05 '19 at 06:39
  • include `#include ` – Phantom Jun 05 '19 at 06:40
  • @Phantom I updated my question with the output I get. I am not sure why it is saying there is no such file when I have the file on my desktop. – Christina Jun 05 '19 at 06:51
  • 1
    @Christina Is it in the directory where you are running your program? – Phantom Jun 05 '19 at 06:52
  • How are you running your code? – melpomene Jun 05 '19 at 06:52
  • @Phantom I am using Clion for the code & it is in the same folder as my project is saved in. – Christina Jun 05 '19 at 06:52
  • @Christina Is it in the same directory that the executable is in? (Also, what OS are you on?) – melpomene Jun 05 '19 at 06:55
  • @Christina In Clion, is there an option to tell it where to find files? In the if, print the current dir (`char *getwd(char *buf);`, in `#include `) – Phantom Jun 05 '19 at 06:57
  • Most IDE's have a setting that determines the current working directory when the code is run. See [How do I change the working directory for my program](https://stackoverflow.com/questions/25834878/) – user3386109 Jun 05 '19 at 06:58
  • @Christina, it appears that the executable (not the project) and the text files are not present in the same directory. Please either keep both the files in the same directory, or give the complete path+file name of the file in the command line. – ARD Jun 05 '19 at 07:00
  • By default, your program runs from `cmake-build-debug`, so that's where it'll look for files. – melpomene Jun 05 '19 at 07:04

0 Answers0