首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用fts遍历文件系统(3)

使用fts遍历文件系统(3)
EN

Stack Overflow用户
提问于 2012-09-27 04:22:38
回答 2查看 7.5K关注 0票数 2

我有一个关于fts(3)的问题。每当我试图访问fts_children()函数的任何成员时,我都会得到一个分段错误。当我阅读http://www.kernel.org/doc/man-pages/online/pages/man3/fts.3.html的手册页时,它声称在read函数运行后填充了它自己,并返回一个通过结构中的link字段链接的链表。我怀疑child_function没有返回任何内容,但我觉得这与手册页不符。我是否应该将这些文件添加到子缓冲区,因为我认为这是自动完成的?我的代码如下,谢谢!

代码语言:javascript
复制
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fts.h>
#include<string.h>

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

int main(int argc, char* const argv[])
{

        FTS* file_system = NULL;
        FTSENT* child = NULL;
        FTSENT* parent = NULL;
        FTSENT* temp = NULL;

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

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

             child = fts_children(file_system,0);
             printf("%s\n", child->fts_path);


        }
//      while (child ->fts_link != NULL)
      //         child = child->fts_link;
        fts_close(file_system);
        return 0;
}

int compare(const FTSENT** one, const FTSENT** two){
        return (strcmp((*one)->fts_name, (*two)->fts_name));
}
"test_fs.c" 43L, 1108C  
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-27 04:51:24

您只需添加一个空检查。

你可能想要

  • add one for file_system
  • 检查命令行参数
  • 添加更多错误处理:

如果成功,fts_children()函数将返回一个指向FTSENT结构的指针,该结构描述目录中以NULL终止的文件链接列表中的第一个条目。fts_children()函数可能会失败,并为chdir()malloc()opendir()readdir()stat()函数指定的任何错误设置errno

更新为评论中的新问题:

  • 用于链表遍历的while循环被放错了位置(在外部循环之外?)
  • 只显示路径...不是文件名。

当你在做的时候:

代码语言:javascript
复制
#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**);

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

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

    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)
            {
                perror("fts_children");
            }

            while ((NULL != child)
                && (NULL != child->fts_link))
            {
                child = child->fts_link;
                printf("%s%s\n", child->fts_path, 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));
}

输出片段示例:

代码语言:javascript
复制
./.profiles/sehe/.opera/icons/cache/g_0000
./.profiles/sehe/.opera/icons/cache/g_0000/opr00002.tmp
./.profiles/sehe/.opera/icons/cache/g_0000/opr00003.tmp
./.profiles/sehe/home/sehe/.mozilla
fts_children: Permission denied
./.vbox-sehe-ipc/lock
票数 2
EN

Stack Overflow用户

发布于 2014-03-12 22:31:03

如果只对遍历指定路径的所有目录和文件感兴趣,只需重复调用fts_read即可。

如果你只想遍历这个流,@sehe的例子可以重写为:

代码语言:javascript
复制
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<fts.h>
#include<string.h>
#include<errno.h>

int compare (const FTSENT**, const FTSENT**);
void indent (int i);

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);
    }

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

    if (NULL != file_system)
    {
        while( (node = fts_read(file_system)) != NULL)
        {
            switch (node->fts_info) 
            {
                case FTS_D :
                case FTS_F :
                case FTS_SL:
                    indent(node->fts_level);
                    printf("%s\n", node->fts_name);
                    break;
                default:
                    break;
            }
        }
        fts_close(file_system);
    }
    return 0;
}

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

void indent(int i)
{
    for (; i > 0; i--) 
        printf("    ");
}

当您运行它时,它会遍历流,并按顺序列出所有文件和目录:

代码语言:javascript
复制
★ mkdir -p test/A/1 test/A/2 test/B/1 test/B/2

★ tree test                                   
test
├── A
│   ├── 1
│   └── 2
└── B
    ├── 1
    └── 2

★ ./fts test                                  
test
    A
        1
        2
    B
        1
        2

仅当需要特定目录的子节点列表时,才调用fts_children。在这种情况下,必须在调用fts_children之前至少调用fts_read一次;否则fts_children将只向fts_open返回argv参数中指定的节点。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12609747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档