即使文件存在(由readdir()返回),C中的fopen()也会返回NULL.

编程入门 行业动态 更新时间:2024-10-25 20:29:51
本文介绍了即使文件存在(由readdir()返回),C中的fopen()也会返回NULL.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图打开一个目录,然后打开一个名为"YouTubeSign"的文件,该目录可以正常打开,但是由于某种原因,即使该文件存在,文件上的fopen()也会失败;有什么想法吗?

I am trying to open a directory, and then open a file named "YouTubeSign", directory opens fine, but for some reason fopen() on the file fails even if the file exists; any ideas?

int main(int argc, char **argv) { DIR *pdir = NULL; struct dirent *in_file; FILE *signature = NULL; pdir = opendir(argv[1]); if (pdir == NULL) { printf("could not open directory %s", argv[1]); return -1; } while (in_file = readdir(pdir)) { if (strcmp(in_file->d_name, "YouTubeSign") == 0) { signature = fopen(in_file->d_name, "r"); printf("opening youtubesign"); break; } } if (signature == NULL){ printf("could not open file "); return -1; }

推荐答案

问题是从readdir获取的文件名 not 中不包含提供给opendir的基本路径.

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.

更多推荐

即使文件存在(由readdir()返回),C中的fopen()也会返回NULL.

本文发布于:2023-11-08 11:39:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569266.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:也会   文件   fopen   readdir   NULL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!