C语言 fopen() 返回空指针,但文件肯定存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6686318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
fopen() returning a NULL pointer, but the file definitely exists
提问by Barjavel
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 指针?
采纳答案by wberry
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('.')
回答by Jacob
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(),然后修复你的相对路径。
回答by Janne
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 没有显示扩展名。
回答by Kevin D.
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 中运行,您可能需要在输入文件名前加上“./”。
回答by karsten
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.
每年我都会遇到类似的问题:我尝试打开一个文件名在字符串中的文件,该文件是从 sting 操作中获得的。当我打印名称时,它似乎没问题,但 fopen() 返回一个空指针。唯一的帮助是打印带有分隔符的名称,显示文件名字符串的确切开头和结尾。当然,这对无法打印的字符没有帮助。
回答by Nautilus
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.
我刚刚遇到了类似的问题,我知道路径正确并且文件位于正确的位置。检查文件权限。程序可能无法访问该文件,因为它的权限被拒绝。
回答by Tron
I encountered the same errno to fopen on Linux from a script file corrupted by Windows.
我在 Linux 上从被 Windows 损坏的脚本文件中遇到了相同的 errno。
ENOENT 2 No such file or directory
ENOENT 2 没有那个文件或目录
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.
Windows 上的写字板(或其他一些 Microsoft 罪魁祸首)将 CRLF = (0x0D, 0x0A) 插入到我的 linux 脚本文件中,而不是换行符 = LF = 0x0A。当我将文件名读入缓冲区并调用 fopen 时,如果由于不可见的附加 CR 字符而失败。
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.
在 Linux Mint 的 Codelite 编辑器中,我能够显示 EOL 字符(查看 > 显示 EOL)并使用查找和替换删除它们,使用从损坏的脚本文件中复制和粘贴 CRLF 并将未损坏文件中的 LF 复制并粘贴到文本中领域。

