问题描述
我在这里搜索了 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 (将#修改为@)