0

我在制作递归目录列表器时遇到的问题之一是在递归函数的堆栈中没有调用目录检查后的调用。

void direct_crawl(int indent, char *cwd, STAT nstat)
{
    int i, i_in, count;
    struct direct **files;
    int file_comp();

    count = scandir(cwd, &files, file_comp, alphasort);

    for (i = 0; i < count; i++)
    {
       for (i_in = 0; i_in < indent; i_in++)
            printf("\t");
       printf("%s\n", files[i]->d_name);

       stat(files[i], nstat);

       if (S_ISDIR(nstat->st_mode) != 0)
            direct_crawl(indent + 1, files[i]->d_name, nstat)
     }

}

它上升了一个子目录,但如果这些目录有子目录,它就不会打扰......所以,有人可以解释一下我做错了什么吗?谢谢。

4

1 回答 1

0

你做错的是,即使在子目录中你stat()只用一个文件名调用,它是在当前目录而不是子目录中搜索的,所以stat()失败了。如果您在循环chdir(cwd)之前和chdir("..")之后调用它可能会起作用。for

于 2014-10-24T11:42:18.420 回答