0

我在使用fts_children() referenced此手册页http://www.kernel.org/doc/man-pages/online/pages/man3/fts.3.html中的功能时遇到了困难。似乎fts_children()并没有像它在手册页中声称的那样获得子目录中的所有文件。例如,我在文件temp夹中有一个文件,其中v2包含许多文件。我得到了子目录中的所有文件,除了按字母顺序排列的文件。对此我能做些什么吗?我的代码在下面,子目录中的文件具有权限。代码:

int compare (const FTSENT**, const FTSENT**);

int main(int argc, char* const argv[])
{
    FTS* file_system = NULL;
    FTSENT* child = NULL;
    FTSENT* parent = NULL;

    file_system = fts_open(argv + 1,FTS_COMFOLLOW | FTS_NOCHDIR,&compare);

    if (NULL != file_system)
    {
        while( (parent = fts_read(file_system)) != NULL)
        {

            child = fts_children(file_system,0);
            if (errno != 0)


        while ((NULL != child) && (NULL != child->fts_link))

            {
                child = child->fts_link;
                printf("%s\n", child->fts_name);
            }

        }
        fts_close(file_system);
    }
    return 0;
}

int compare(const FTSENT** one, const FTSENT** two)
{
    return (strcmp((*one)->fts_name, (*two)->fts_name));
}

临时文件(使用 ls -l 输出):

total 8
-rwxrwxrwx  1 tparisi student 14 Sep 27 13:05 a
-rw-r--r--+ 1 tparisi student 25 Sep 26 14:42 f
-rw-r--r--+ 1 tparisi student 13 Sep 27 11:28 file2
-rw-r--r--+ 1 tparisi student 14 Sep 27 11:42 files
drwxr-xr-x+ 2 tparisi student  3 Sep 27 13:33 temp2
-rw-r--r--+ 1 tparisi student 14 Sep 27 13:04 test
-rw-r--r--+ 1 tparisi student  5 Sep 27 13:23 z

a当我运行我的程序时,整个目录都打印出来接受。

任何帮助都会很棒!谢谢!

4

1 回答 1

1

遍历返回的列表时跳过第一个条目:

while ((NULL != child) && (NULL != child->fts_link))
{
    child = child->fts_link;
    printf("%s\n", child->fts_name);
}

尝试

while(child)
{
    printf("%s\n", child->fts_name);
    child = child->fts_link;
}

从打印的返回列表中获取所有文件。

于 2012-09-27T18:35:30.320 回答