0

我在子文件夹中读取文件时遇到问题。我在不同的文件夹中有大约 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=%s\n",src);
    while (p < end  &&  *src != '\0') {
        *p++ = *src++;
    }
    *p = '\0';

    /* 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 != '\0') {
                *q++ = *src++;
            }
            *q = '\0';

            /* Decide what to do with the directory entry */
            switch (ent->d_type) {
                case DT_REG:
                    /* Output file name with directory */
                    {
                        printf ("FILE=%s\n", 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 %s\n", 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; 
    }

}
4

2 回答 2

0

也加入了 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 %s\n", newpath);
            /* readfile(newpath); */

            free(newpath);
        }
      }
   }
      closedir(dir); /* Add this <--- */
}
于 2014-01-30T11:12:12.740 回答
0

像这样的东西怎么样:

/*
 * 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 %s\n", newpath);
                /* readfile(newpath); */

                free(newpath);
            }
        }
    }
}
于 2013-03-06T15:51:22.000 回答