fopen()函数返回NULL指针,但该文件存在绝对 [英] fopen() returning a NULL pointer, but the file definitely exists

查看:3061
本文介绍了fopen()函数返回NULL指针,但该文件存在绝对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code我有如下:

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

文件'的data.txt'在工作目录(它存在于包含我的.c和.h文件的目录),所以为什么fopen()函数返回一个空指针?肯定是存在的。

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?

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

在Unix上,文件名是真正字节字符串不是字符串,它可以创建与对照文件,如在其名称中退格。我已经看到在过去的案件中,复制粘贴到终端的结果与平常的名字,但试图打开出现在目录列表中导致错误的文件名。

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

这篇关于fopen()函数返回NULL指针,但该文件存在绝对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆