本文介绍了fopen()返回“没有这样的文件或目录".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在C语言中的fopen()函数有问题.
I have some problem with fopen() function in C.
我解析了目录,并将所有路径放入char数组(char **).之后,我应该打开所有这些文件.和...fopen对于某些文件返回无此文件或目录".我真的不明白,为什么.
I'am parsed directory and put all the paths to char array(char**). After that i should to open all these files. And...fopen returns "No such file or directory" for some files. And I Am really don't understand, why.
- 所有路径都是正确的.我检查了.
- 我有所有特权 打开这些文件.
- 如果我从错误日志复制路径到文件并尝试 通过我的程序仅打开此文件-它可以工作.
- 其他 程序不能使用这些文件(我认为).
- All paths are right. I checked it.
- I have all privileges to open these files.
- If I copy path to file from error log and try to open only this file via my programm - it works.
- Others programms don't work with these files(i think).
我该怎么办?
int main(int argc, char *argv[]){
char** set = malloc(10000*sizeof(char*));
char* path = argv[1];
listdir(path, set); /* Just parse directory. Paths from the root. No problem in this function. all paths in the variable "set" are right */
int i=0;
while(i<files){ /* files is number of paths */
FILE* file = fopen(set[i++],"rb");
fseek(file, 0L, SEEK_END);
int fileSize = ftell(file);
rewind(file);
/*reading bytes from file to some buffer and close current file */
i++;
}
}
推荐答案
- 您将"i"递增两次.可能会误会吗?
- 您可以不使用stat()打开文件而获得文件大小.
- ftell()返回"long",请勿将其强制转换为"int",因为它可以缩短并且会丢失正确的值.
尝试以下代码:
#include <stdio.h>
#include <sys/stat.h>
/* example of listdir, replace it with your real one */
int listdir(const char *path, char *set[])
{
set[0] = "0.txt";
set[1] = "1.txt";
set[2] = "2.txt";
set[3] = "3.txt";
set[4] = "4.txt";
return 5;
}
int main(int argc, char *argv[]) {
int files;
char *path = argv[1];
char **set = malloc(1000 * sizeof(char *));
files = listdir(path, set);
for (int i = 0; i < files; i++) {
struct stat st;
stat(set[i], &st);
printf("FileSize of %s is %zu\n", set[i], st.st_size);
}
free(set);
}
这篇关于fopen()返回“没有这样的文件或目录".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!