4

对于这个 fts_children() 问题,我一直在敲我的头。在手册页http://www.kernel.org/doc/man-pages/online/pages/man3/fts.3.html中,它清楚地说明As a special case, if fts_read() has not yet been called for a hierarchy, fts_children() will return a pointer to the files in the logical directory specified to fts_open(), that is, the arguments specified to fts_open(). 了我认为其中所有文件的链接列表返回当前目录。好吧,我发现情况并非如此,我非常感谢在这件事上提供一些帮助。我希望返回一个链接列表,然后我会遍历它以找到具有匹配文件名的文件(最终目标)。但是,现在,我只是想遍历链表(小步骤)。现在,它将返回一个文件,然后退出循环。这对我来说没有意义。任何帮助将不胜感激!!!

文件系统的打开:

char* const path[PATH_MAX] = {directory_name(argv[argc-index]), NULL}; 
            char* name = file_name(argv[argc-index]);

            if ((file_system = fts_open(path, FTS_COMFOLLOW, NULL)) == NULL){
                fprintf(stderr,"%s:%s\n", strerror(errno), getprogname());
                        exit(EXIT_FAILURE);
                 }/*Ends the files system check if statement*/

            /*Displays the information about the specified file.*/
            file_ls(file_system,name, flags);

为澄清起见,directory_name 解析用户输入的路径并返回类似 /home/tpar44 的内容。然后打开该目录。

在文件系统中搜索:

void
file_ls(FTS* file_system, char* file_name,  int* flags){
    FTSENT* parent = NULL;
    //dint stop = 0;

    parent = fts_children(file_system, 0);

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

谢谢!

4

1 回答 1

4

我认为这完全是设计使然。

...也就是说,指定给 fts_open() 的参数...

它说的是它会在path_argv参数中列出根元素以方便您。它将path_argv数组本身视为一个逻辑目录

换句话说:

int main(int argc, char* const argv[])
{
    char* const path[] = { ".", "/home", "more/root/paths", NULL }; 

    FTS* file_system = fts_open(path, FTS_COMFOLLOW | FTS_NOCHDIR, &compare);

    if (file_system)
    {
        file_ls(file_system, "", 0);
        fts_close(file_system);
    }
    return 0;
}

将输出

parent = .
parent = /home
parent = more/root/paths

事实上,确实如此(参见http://liveworkspace.org/code/c2d794117eae2d8af1166ccd620d29eb)。

这是一个更完整的示例,显示了完整的目录遍历:

#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fts.h>
#include<string.h>
#include<errno.h>

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

void file_ls(FTS* file_system, const char* file_name, int* flags)
{
    FTSENT* node = fts_children(file_system, 0);

    if (errno != 0)
        perror("fts_children");

    while (node != NULL)
    {
        // TODO use file_name and flags
        printf("found: %s%s\n", node->fts_path, node->fts_name);
        node = node->fts_link;
    }
}

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

    if (argc<2)
    {
        printf("Usage: %s <path-spec>\n", argv[0]);
        exit(255);
    }

    char* const path[] = { argv[1], NULL }; 
    const char* name = "some_name";

    file_system = fts_open(path, FTS_COMFOLLOW | FTS_NOCHDIR, &compare);

    if (file_system)
    {
        file_ls(file_system, name, 0); // shows roots

        while( (node = fts_read(file_system)) != NULL)
            file_ls(file_system, name, 0); // shows child elements

        fts_close(file_system);
    }
    return 0;
}

int compare(const FTSENT** one, const FTSENT** two)
{
    return (strcmp((*one)->fts_name, (*two)->fts_name));
}
于 2012-10-06T21:48:13.650 回答