1

I am trying to create some code where a user inputs the name of a file, and the program then travels a couple of folders up, and then into a separate folder called "Files", finds the file, and echoes it.

void readfile() {
    FILE *file;
    int c;
    char str[30] = "..\\..\\Files";
    char temp[30];
    printf("Please enter the name of the file\n");
    fgets(temp, sizeof (temp), stdin);
    strcat(str, temp);
    file = fopen(str, "r");
    if (file) {
        while ((c = getc(file)) != EOF)
            putchar(c);
        fclose(file);
    } else {
        printf("File does not exist!");
    }
    //printf(string);
    fclose(file);
}

However, when it tries to open up a file, the program crashes in Netbeans, but it returns the "File does not exist" error in CLion. I am very confused and I cannot pinpoint the problem.

  • 2
    You must remove the newline that `fgets` probably includes at the end of `temp`. Please [read this answer](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221). – Weather Vane Jul 19 '17 at 20:50
  • 1
    1) don't bean-count. [256], say, for temp. 2) strcpy the path to temp, then strcat the name to it. 3) printf() out the full path+ filename spec so you know it's right. – Martin James Jul 19 '17 at 20:51
  • 1
    ...and for `str` too, you already used up half its niggardly length which you `strcat` to. Even better when developing use strings of 1000 length and tone them down later. – Weather Vane Jul 19 '17 at 20:52
  • 2
    Oh.. and you need another '\\', something you would have seen if you had printed out the whole file spec. – Martin James Jul 19 '17 at 20:54
  • We are off topic here: give the absolute program path. I am sure this has been asked many times. Where is the working folder? – Weather Vane Jul 19 '17 at 20:56
  • @MartinJames - the most egregious error of them all :) – KevinDTimm Jul 19 '17 at 21:00
  • The working folder is on a Linux server, and I am using a remote build to a Linux workstation to run it, using Putty to check if the files are created. But I am not using remote build on the CLion project where I test the code. It crashes on the Linux remote run and not the local CLion run, which really confuses me. – Andrew Haworth Jul 20 '17 at 09:49
  • this is the source of your problem: `strcat(str, temp);` because `str` is already over 1/2 full and `temp` is (possibly) another 30 characters. so 16+30 => 46 – user3629249 Jul 22 '17 at 07:20
  • there are several different reasons a call to `fopen()` could fail. this line: `printf("File does not exist!");` has two problems 1) it might not be the correct reason 2) error messages are to be output to `stderr`, not `stdout`. Suggest using: `perror( "fopen failed" );` Then it will output to `stderr` and will include a message saying why the OS thinks the call failed. – user3629249 Jul 22 '17 at 07:25
  • the code calls: `fclose( file );` twice in the same execution path. This is an error, especially if the call to `fopen() failed. – user3629249 Jul 22 '17 at 07:27

0 Answers0