读取子文件夹中的许多文件时出错

Error reading many files in a subfolder

本文关键字:文件 出错 许多 文件夹 读取      更新时间:2023-10-16

我在子文件夹中读取文件有问题。我在不同的文件夹里有大约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 <--- */
}