Donkey Xot
Donkey Xot

Reputation: 23

Fopen - No such file or directory in C

This code does not open file properly, it returns no such file or directory, although the path and privilege are there and no other program is using the file. How can I fix the error? I tried swapping the path and moving the file, the error is there still.

    char string[105];
    FILE* file = fopen("C:\\Users\\Public\\Documents\\a.txt", "r");
    while (fgets(string, 100, file)) {
        printf("%s", string);
    }

Upvotes: 2

Views: 9996

Answers (1)

Steve Summit
Steve Summit

Reputation: 47915

It can be surprisingly tricky to open a simple file! Lots of things can go wrong. I recommend writing slightly more verbose code, like this:

#include <string.h>
#include <errno.h>

char* filename = "C:\\Users\\Public\\Documents\\a.txt";
char string[105];
FILE* file = fopen(filename, "r");
if (file == NULL) {
    fprintf(stderr, "can't open %s: %s\n", filename, strerror(errno));
    exit(1);
}
while (fgets(string, 100, file)) {
    printf("%s", string);
}

The point is that the error message prints out both the name of the file it tried but failed to open, and the actual reason it couldn't open it. (Also, by storing the filename in a variable, you make it absolutely certain that the filename it prints in the error message is the filename it tried but failed to open.) It sounds like you already know that the error was "no such file or directory", but I'm not sure how you know this.

Even though I've been programming in C for a long time, sometimes I still have this problem. One thing I'll sometimes do is use my mouse to copy the exact filename string printed in the error message, the file the program said it couldn't open, the file I'm sure is really there, and paste it into a terminal window (or CMD or Powershell if you're on Windows), along with a directory-listing command, to see if the operating system can actually see the file. That is, for your example, the command I'd run is

dir C:\Users\Public\Documents\a.txt

but the point is that I would not actually type the pathname "C:\Users\Public\Documents\a.txt", instead I would copy and paste it out of the error message that the program printed. Sometimes there are surprising little impossible-to-spot differences between the filename you thought it was trying to open, versus the filename it was actually trying to open, and this exercise is a good way to let the computer help you find those differences.

Remember, too, that you'll get the error "No such file or directory" if there's no file by that name in the directory, or if the directory isn't there at all. For example, if you're trying to open the path

C:\Users\Public\Documents\a.txt

and the file a.txt keeps not being there, and you keep checking your home directory

C:\Users\Donkey\Documents

and you keep seeing that the file is there, it can be surprisingly easy to overlook what the real problem is. :-)


Addendum: You might be having an issue with the different Unix/Linux versus Windows file path separators, that is, / versus \. Usually, on a Windows machine, it's safest to use \, as you've done. (One very frequent mistake is to forget to double the backslashes, but it looks like you got that right.) Depending on your programming environment, if there's some level of Unix emulation going on, you can sometimes use Unix-style /, and it will automatically translate to \ for you. I've never heard of a situation where using \ made it not work (which is a possibility being explored in the comments), but you might experiment with that, perhaps trying

char* filename = "/c/Users/Public/Documents/a.txt";

or (less likely)

char* filename = "C:/Users/Public/Documents/a.txt";

Upvotes: 4

Related Questions