fopen()返回一个NULL指针,但该文件肯定存在

时间:2021-05-11 13:25:41

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?

文件'data.txt'肯定存在于工作目录中(它存在于包含我的.c和.h文件的目录中),那么为什么fopen()返回一个NULL指针?

6 个解决方案

#1


7  

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

文件名是否可能不是“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.

在Unix上,文件名实际上是字节字符串而不是字符串,并且可以在名称中创建带有控件(如退格)的文件。我已经看到过去复制粘贴到终端导致文件具有普通名称的情况,但是尝试打开目录列表中出现的文件名会导致错误。

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

一种方法可以确定文件名确实是您认为的文件名:

$ python
>>> import os
>>> os.listdir('.')

#2


9  

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.

即尝试先用完整的绝对路径打开它;如果它工作,那么你只需要弄清楚_getcwd()当前目录是什么,然后修复你的相对路径。

#3


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.

确保输入文件与可执行文件位于同一目录中,该目录可能与保存源文件的目录不同。如果您在IDE调试器中运行该程序,请确保将您的工作目录设置为输入文件的位置。此外,如果您在* nix而不是Windows中运行,则可能需要在输入文件名前加上“./”。

#4


2  

Invisible SPACE character in file name?

文件名中有不可见的SPACE字符?

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.

一年一次,我遇到了类似的问题:我尝试打开一个文件名的文件,从一个sting操作获得。当我打印名称似乎没问题,但fopen()返回一个空指针。唯一的帮助是使用分隔符打印名称,显示文件名字符串的确切开头和结尾。当然,这对不可打印的字符没有帮助。

#5


2  

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.

我的问题是我有一个文件filename.txt,我没有意识到实际上它是filename.txt.txt,因为Windows没有显示扩展名。

#6


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.

我刚才有类似的问题,我知道路径是正确的,文件位于正确的位置。检查文件权限。程序可能无法访问该文件,因为它正在获得权限被拒绝。

#1


7  

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

文件名是否可能不是“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.

在Unix上,文件名实际上是字节字符串而不是字符串,并且可以在名称中创建带有控件(如退格)的文件。我已经看到过去复制粘贴到终端导致文件具有普通名称的情况,但是尝试打开目录列表中出现的文件名会导致错误。

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

一种方法可以确定文件名确实是您认为的文件名:

$ python
>>> import os
>>> os.listdir('.')

#2


9  

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.

即尝试先用完整的绝对路径打开它;如果它工作,那么你只需要弄清楚_getcwd()当前目录是什么,然后修复你的相对路径。

#3


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.

确保输入文件与可执行文件位于同一目录中,该目录可能与保存源文件的目录不同。如果您在IDE调试器中运行该程序,请确保将您的工作目录设置为输入文件的位置。此外,如果您在* nix而不是Windows中运行,则可能需要在输入文件名前加上“./”。

#4


2  

Invisible SPACE character in file name?

文件名中有不可见的SPACE字符?

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.

一年一次,我遇到了类似的问题:我尝试打开一个文件名的文件,从一个sting操作获得。当我打印名称似乎没问题,但fopen()返回一个空指针。唯一的帮助是使用分隔符打印名称,显示文件名字符串的确切开头和结尾。当然,这对不可打印的字符没有帮助。

#5


2  

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.

我的问题是我有一个文件filename.txt,我没有意识到实际上它是filename.txt.txt,因为Windows没有显示扩展名。

#6


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.

我刚才有类似的问题,我知道路径是正确的,文件位于正确的位置。检查文件权限。程序可能无法访问该文件,因为它正在获得权限被拒绝。