0

我试图递归地获取所有文件和文件夹列表。但是我只能获取文档的子目录及其内部。我无法获取子目录内部的其他文件夹。我不知道如何递归。我希望你能帮助我

          #include <stdio.h>
     #include <sys/types.h>
     #include <dirent.h>
     #include <windows.h>
     #include <unistd.h>
     #include <string.h>
     void list(char *a);
     void reader(char *path);   
     int
     main (void)
     {
       DIR *dp;
       struct dirent *ep;

       dp = opendir ("C:\\Users\\pen\\Documents\\");
       if (dp != NULL)
         {
           while (ep = readdir (dp)){

GetFileAttributes(ep->d_name); 
if(FILE_ATTRIBUTE_DIRECTORY & GetFileAttributes(ep->d_name))
{

        if (strcmp(".",ep->d_name)==0)
            continue;

    if (strcmp("..",ep->d_name)==0)
    continue;



     reader(ep->d_name);

}

             }
             closedir(dp);

         }
       else
         perror ("Couldn't open the directory");
         closedir(dp);
     system("pause");
       return 0;
     }
    void reader(char *path){
            DIR *da;
            struct dirent *ef;
                da = opendir(path);
            while (ef=readdir(da)){
            printf ("%s\n",ef->d_name);
        if(FILE_ATTRIBUTE_DIRECTORY & GetFileAttributes(ef->d_name))
        {

if (strcmp(".",ef->d_name)==0)
continue;


    if (strcmp("..",ef->d_name)==0)
continue;
    reader(ef->d_name);

}
    }
      closedir(da);
}
4

1 回答 1

1

1)在reader你需要closedir(da);在while循环之后调用。

2)每次调用都reader需要有你需要连接的绝对路径path

ef->d_name然后打电话给读者。

3)还要启用调试,您应该在调用 perror失败后readdir调用。

于 2012-04-08T07:46:48.117 回答