fopen() in C returns NULL even when file exists (is returned by readdir())

﹥>﹥吖頭↗ 提交于 2019-12-02 03:23:48

The problem is that the file-name you get from readdir does not include the base path provided to opendir.

You have to create the full path using the base path, the path separator and the file-name.

You have to pass an absolute file name when you use fopen(), composed with the directory name and file base name, e.g.:

char abs_path[PATH_MAX];
// ...
snprintf(abs_path, PATH_MAX, "%s/%s", argv[1], in_file->d_name);
signature = fopen(abs_path, "r");

The problem is that it will work from the command line when you're testing this code. There's nothing technically missing here. You need to make sure that the process is using the correct working directory.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!