fopen() returning a NULL pointer, but the file definitely exists

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.


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('.')

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.