20

The code I have is as follows:

FILE *txt_file = fopen("data.txt", "r");
if (txt_file == NULL) {
    perror("Can't open file");
} 

The error message returned is:

Can't open file: No such file or directory

The file 'data.txt' definitely exists in the working directory (it exists in the directory that contains my .c and .h files), so why is fopen() is returning a NULL pointer?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Barjavel
  • 1,626
  • 3
  • 19
  • 31
  • 7
    are you executing from the command line or from an IDE? If IDE, are you sure the program's working directory is set to the directory containg the datafile? – fvu Jul 13 '11 at 22:12
  • 1
    can you add the output from `ls -Fal` for the directory in question? – Fredrik Pihl Jul 13 '11 at 22:12
  • 1
    you maybe don't have permission to open it. are you on linux? – Vinicius Kamakura Jul 13 '11 at 22:15
  • Can you attach the whole code? – user482594 Jul 13 '11 at 22:15
  • What's the current directory of the application? Have you tried it with an absolute path? Is the file opened for writing by someone else? Is there a permissions issue? What operating system and file system is this? – EboMike Jul 13 '11 at 22:15
  • @fvu I'm executing from an IDE: Visual Studio 2008. I'll have to lookup how to check its working directory. – Barjavel Jul 13 '11 at 22:16
  • Try the full pathname: `#define FULLPATHTOFILES "C:\\Users\\Barjavel\\Projects\\source\\bin\\debug\\"` and `txt_file = fopen(FULLPATHTOFILES "data.txt", "r");` Notice I ended the macro text with a final `'\'` – pmg Jul 13 '11 at 22:18
  • 1
    You can get the current working directory with the \_getcwd() function, use that for debugging and learning the working directory Visual Studio runs your program in. – nos Jul 13 '11 at 22:23
  • I'm on Windows 7. I have tried the absolute path, of the form "C:/User/SomeProject/source/bin/debug/somedirectory" which returns the same error. – Barjavel Jul 13 '11 at 22:29

7 Answers7

16

Standard problem. Try

FILE *txt_file = fopen("C:\\SomeFolder\\data.txt", "r");

I.e. try opening it with the full absolute path first ; if it works then you just have to figure out what the current directory is with _getcwd() and then fix your relative path.

Jacob
  • 34,255
  • 14
  • 110
  • 165
11

Is it possible that the filename is not really "data.txt"?

On Unix, filenames are really byte strings not character strings, and it is possible to create files with controls such as backspace in their names. I have seen cases in the past in which copy-pasting into terminals resulted in files with ordinary-looking names, but trying to open the filename that appears in a directory listing results in an error.

One way to tell for sure that the filenames really are what you think they are:

$ python
>>> import os
>>> os.listdir('.')
wberry
  • 18,519
  • 8
  • 53
  • 85
5

My problem was that I had a file filename.txt and I didn't realize that in reality it was filename.txt.txt due to windows not showing the extension.

Janne
  • 51
  • 1
  • 1
2

Invisible SPACE character in file name?

Once a year I have a similar problem: I try to open a file with the filename in a string, obtained from a sting operation. When I print the name it seems OK, but fopen() returns a null pointer. The only help is printing the name with delimiters showing the exact beginning and end of the filename string. Of course this does not not help with unprintable chars.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
karsten
  • 639
  • 5
  • 13
2

Make sure that your input file is in the same directory as the executable, which may be different than the one where your source files are kept. If you're running the program in an IDE debugger, make sure that your working directory is set to the location of the input file. Also, if you're running in *nix rather than Windows, you may need to prepend a "./" to the input filename.

Kevin D.
  • 911
  • 1
  • 6
  • 18
  • 3
    There is no reason to prepend `./` to a filename passed to `fopen()` on UNIX. – caf Jul 13 '11 at 23:07
0

I just had a similar issue like this where I knew the path was correct and the file was in the right location. Check the file permissions. It is possible that the program cannot access the file because it is getting permission denied.

Nautilus
  • 105
  • 10
0

I encountered the same errno to fopen on Linux from a script file corrupted by Windows.

ENOENT 2 No such file or directory

Wordpad on Windows (or some other Microsoft culprit) inserted CRLF = (0x0D, 0x0A) into my linux script files in place of newline = LF = 0x0A. When I read the file name into a buffer and called fopen if failed due to the invisible appended CR character.

In the Codelite editor on Linux Mint I was able to show EOL characters (View > Display EOL) and remove them with find and replace, using copy and paste of the CRLF from the corrupted script files and the LF from an uncorrupted file into the text fields.

Tron
  • 21
  • 3