如何解决C 中 fopen 方法的无效参数错误
我在这里搜索了 15-20 个类似的问题,但没有一个能解决我的问题,我仍然遇到相同的错误。
这里我想读取位于 Text_Files 文件夹中的 .txt 文件。(有 3 个 .txt 文件;doc1.txt、doc2.txt、doc3.txt)
我收到“没有这样的文件或目录”或“无效参数”错误。我知道路径和/或我将其放入 fopen 方法的方式有问题。
int main()
{
DIR *dir;
FILE* entry_file;
struct dirent *in_file;
char toOpenFiles[1000];
dir = opendir("E:\\C Projects\\Term_Project_2\\Cprojects\\Text_Files");
while ((in_file = readdir(dir)) != NULL)
{
//this part is to be able get file name(It works correctly)
if (!strcmp(in_file->d_name, "."))
continue;
if (!strcmp(in_file->d_name, ".."))
continue;
//For path I also tried E:\\C Projects\\Term_Project_2\\Cprojects\\ but same error occurs
strcpy(toOpenFiles, "\\E:\\C Projects\\Term_Project_2\\Cprojects\\");
// in_file->d_name returns doc1.txt
strcat(toOpenFiles, in_file->d_name);
full_path[0]='\0';
strcat(full_path,toOpenFiles);
//Here entry_file 's value is 0x0 for some reason
entry_file = fopen(full_path, "r");
if (!entry_file)
{
//this prints 'No such file or directory' if I put full_path as a parameter in fopen or 'Invalid argument' if I put toOpenFiles
printf("Error : errno='%s'.\n",strerror(errno));
}
else
{
printf("It's working!");
}
closedir(dir);
return 0;
}
编辑 当我用这个改变路径时就解决了:
strcpy(toOpenFiles,"E:\\C Projects\\Term_Project_2\\Cprojects\\Text_Files\\");
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。