读取子文件夹中的许多文件时出错
Error reading many files in a subfolder
我在子文件夹中读取文件有问题。我在不同的文件夹里有大约6000个不同的文件。我将阅读每个文件。但是如果我读了大约2000个文件,那么应用程序就不是问题了。当我读了6000个文件,这意味着整个子文件夹。应用程序将显示"无法打开文件"的问题。但如果我只访问未打开的文件夹,则应用程序没有问题。我不知道发生了什么?我想可能是我读了很多文件,内存不够。你能帮我编辑一下吗?
//This is code to access subforder
static int
find_directory(
const char *dirname)
{
DIR *dir;
char buffer[PATH_MAX + 2];
char *p = buffer;
const char *src;
const char* folder_dir;
char *end = &buffer[PATH_MAX];
int ok;
/* Copy directory name to buffer */
src = dirname;
printf("src=%sn",src);
while (p < end && *src != ' ') {
*p++ = *src++;
}
*p = ' ';
/* Open directory stream */
dir = opendir (dirname);
if (dir != NULL) {
struct dirent *ent;
/* Print all files and directories within the directory */
while ((ent = readdir (dir)) != NULL) {
char *q = p;
char c;
/* Get final character of directory name */
if (buffer < q) {
c = q[-1];
} else {
c = ':';
}
/* Append directory separator if not already there */
if (c != ':' && c != '/' && c != '') {
*q++ = '/';
}
/* Append file name */
src = ent->d_name;
while (q < end && *src != ' ') {
*q++ = *src++;
}
*q = ' ';
/* Decide what to do with the directory entry */
switch (ent->d_type) {
case DT_REG:
/* Output file name with directory */
{
printf ("FILE=%sn", buffer);
OFBool check= readfile(buffer)
}
break;
case DT_DIR:
/* Scan sub-directory recursively */
if (strcmp (ent->d_name, ".") != 0
&& strcmp (ent->d_name, "..") != 0) {
find_directory (buffer,opts);
}
break;
default:
/* Do not device entries */
/*NOP*/;
}
}
closedir (dir);
ok = 1;
} else {
/* Could not open directory */
printf ("Cannot open directory %sn", dirname);
ok = 0;
}
return ok;
}
OFBool readfile(const char* filepath)
{
FILE *f=NULL;
OFBool ok = OFFalse;
if( ( f = fopen( filepath, "rb" ) ) == NULL ) // checks to see if file exists
{
ok = OFFalse;
cout<<"can not read file"<<filepath<<endl;
return ok;
}
else
{
ok = true;
cout<<" reading OK"<<endl;
fclose(f);
return ok;
}
}
这样如何:
/*
* Recursively walk though a directory tree.
*
* Arguments:
* path - The root directory to start from
* hidden - If non-zero, include hidden files and directories
*/
void recurse_directory(const char *path, const int hidden)
{
DIR *dir = opendir(path);
if (dir == NULL)
{
perror("opendir");
return;
}
struct dirent *ent;
while ((ent = readdir(dir)) != NULL)
{
if (ent->d_type == DT_DIR)
{
if (strcmp(ent->d_name, ".") != 0 &&
strcmp(ent->d_name, "..") != 0 &&
(ent->d_name[0] != '.' || hidden))
{
char *newpath = malloc(strlen(path) + 1 + strlen(ent->d_name) + 1);
strcpy(newpath, path);
strcat(newpath, "/");
strcat(newpath, ent->d_name);
recurse_directory(newpath, hidden);
free(newpath);
}
}
else if (ent->d_type == DT_REG)
{
if (ent->d_name[0] != '.' || hidden)
{
char *newpath = malloc(strlen(path) + 1 + strlen(ent->d_name) + 1);
strcpy(newpath, path);
strcat(newpath, "/");
strcat(newpath, ent->d_name);
printf("File %sn", newpath);
/* readfile(newpath); */
free(newpath);
}
}
}
}
再加上Joachim Pileborg的回答。如果您正在处理大量子目录,则需要在while循环后添加closedir()调用。这是因为一次可以打开的目录数量是有限制的。上面的例子在处理一个小文件夹时可以很好地工作,但是当处理2000多个子文件夹时,你需要在打开另一个文件夹之前关闭该目录。
/*
* Recursively walk though a directory tree.
*
* Arguments:
* path - The root directory to start from
* hidden - If non-zero, include hidden files and directories
*/
void recurse_directory(const char *path, const int hidden)
{
DIR *dir = opendir(path);
if (dir == NULL)
{
perror("opendir");
return;
}
struct dirent *ent;
while ((ent = readdir(dir)) != NULL)
{
if (ent->d_type == DT_DIR)
{
if (strcmp(ent->d_name, ".") != 0 &&
strcmp(ent->d_name, "..") != 0 &&
(ent->d_name[0] != '.' || hidden))
{
char *newpath = malloc(strlen(path) + 1 + strlen(ent->d_name) + 1);
strcpy(newpath, path);
strcat(newpath, "/");
strcat(newpath, ent->d_name);
recurse_directory(newpath, hidden);
free(newpath);
}
}
else if (ent->d_type == DT_REG)
{
if (ent->d_name[0] != '.' || hidden)
{
char *newpath = malloc(strlen(path) + 1 + strlen(ent->d_name) + 1);
strcpy(newpath, path);
strcat(newpath, "/");
strcat(newpath, ent->d_name);
printf("File %sn", newpath);
/* readfile(newpath); */
free(newpath);
}
}
}
closedir(dir); /* Add this <--- */
}
相关文章:
- 读取文件的最后一行并输入到链接列表时出错
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 在 C/C++ 中打开驻留在 "/sys/" 下的文件时出错
- 在头文件中定义变量不会出错
- 如何在多写入器情况下对文件支持的共享内存中的大页面出错
- JNI,使用两个 .so 文件时出错,其中一个文件需要另一个文件
- 生成文件中隐式规则中的 -c 标志出错
- 编译时出错 - 链接.cpp和头文件
- 从人员矢量插入和读取文件时出错
- LNK2019 在文件中调用静态成员的方法时出错.cpp
- ./main:加载共享库时出错:libopencv_highgui.so.4.0:无法打开共享对象文件:没有这样的文件或
- 猫鼬 - 包含标准C++库文件时出错
- 在f2c.h文件中定义min()max()宏时出错
- ASIOSDK;从hostsample.cpp生成mex文件时出错
- 使用emscripten将c++文件转换为wasm时出错
- 编译后可执行文件出错
- 头文件C++出错
- ftell 非常大的文件出错
- 从磁盘读取映像文件出错