'No such file or directory.' but file is there

676 Views Asked by At

As the title implies I am trying to open a text file in the same directory as of the program I'm running. Here is the code I am using:

int main(int argc, char *argv[]){

    FILE *filePtr;
    filePtr = fopen("something.txt", "r");
    if (filePtr == NULL){
      printf("Oh dear, something went wrong with read()! %s\n", strerror(errno));
      return 1;
    }

  return 0;
}

This prints out:

Oh dear, something went wrong with read()! No such file or directory

I have also tried using fopen("./something.txt", "r"); but the same thing happened.

1

There are 1 best solutions below

0
UpendraG On

1> Go in directory where program + executable is present. Execute from there. 2> If not then there is probability of some permission related issue. try using [ int access(const char *pathname, int mode)] API if(access("something.txt", R_OK) == 0)

then only go ahead. This will check whether you have read permission or not. If you have root permission then only try to open. If not provide sufficient permission.