2

I have this very simple code:

#include <stdio.h>
#include <stdlib.h>

int main (void)
    {
    FILE * file_ptr = NULL;

    file_ptr = fopen ("file.txt", "r");

    if (file_ptr == NULL)
        {
        puts ("Error!");
        return EXIT_FAILURE;
        }
    else
        {
        puts ("O.k.!");
        }

    return EXIT_SUCCESS;
    }

Output:

Error!

Why fopen doesn't work? The file is not protected, not opened elsewhere and is stored in the same folder as the *.exe of this program. I also tried it with giving the complete path to the file and with an array, in which the filename is stored. Everytime it puts out "Error!".

What's going on??

I'm using Eclipse Neon.2 Release (4.6.2) with newest cygwin gcc compiler on Windows 10 64bit.

Thank you for your help!

user3386109
  • 34,287
  • 7
  • 49
  • 68
a kind person
  • 329
  • 1
  • 6
  • 17
  • 3
    Why don't you check `errno` for the *reason* `fopen()` failed? – EOF Jan 30 '17 at 20:32
  • 2
    You might also try `fopen("xxyyzzqq.txt", "w")` and then search the hard drive to see where the file was created. – user3386109 Jan 30 '17 at 20:53
  • 2
    @user3386109: Nice idea! It turned out, that my new Eclipse installation wants the file in the src-directory and not in the debug-directory (where the *exe-file is), where my old installation wanted it. Thank you so much! This cost me the whole day...I'm a beginner. – a kind person Jan 30 '17 at 21:05
  • @EOF: Because I'm a beginner and does not know what "errno" is. I will research it, thank you. – a kind person Jan 30 '17 at 21:16
  • Good to hear, glad I could help! Since you figured it out, the best thing to do is post your own answer, and then accept your answer as the correct answer. In this case, I'll post a wiki answer for you to accept. – user3386109 Jan 30 '17 at 21:18
  • O.k., next time I will do so. Thanks again. – a kind person Jan 30 '17 at 21:28

2 Answers2

3

The problem was solved by changing the fopen to

file_ptr = fopen("xxyyzzqq.txt", "w");

and then searching the hard drive to see where the file was created.

Turns out that the file was created in the project source directory, and not the debug directory (where the .exe file is), unlike the old installation which used the debug directory as the working directory.

user3386109
  • 34,287
  • 7
  • 49
  • 68
1

perror might help.

FILE *file_ptr = fopen("file.txt", "r");
if (!file_ptr) {
  perror("fopen");
} else {
  printf("It's working!");
}

Similar question: fopen() not working in C

Community
  • 1
  • 1
Barry
  • 1,143
  • 2
  • 13
  • 26
  • Thank you, your code puts out: "fopen: No such file or directory". I tried it again with giving the full path to the file and realised, that I have to use "\\" instead of "\" for this ("P:\\Office\\Eclipse C(++)\\workspace\\Test\\Debug\\file.txt"). Then Eclipse finds the file. But it should also work without the path, when the file is in the same folder like the program, but it does not. Thats a problem, because I have to write a program, in witch the user can enter the filename with fgets(). It's not a good solution, when it's necessary to type in the full path. – a kind person Jan 30 '17 at 20:55
  • Eclipse is probably running the program in a different working directory than where you've placed your input file. – David Choweller Jan 30 '17 at 21:06
  • Thanks to the user "user3386109" the problem is solved, but I thank you too for your help, David. – a kind person Jan 30 '17 at 21:08