保持ftw功能的目录搜索结果

时间:2011-10-23 04:01:45

标签: c libc

我要做的是递归获取" .mp3"来自确定的预先指定目录及其子目录的归档。我没有问题获取mp3并在控制台上打印它们。我正在使用http://www.gnu.org/software/libc/manual/html_node/Working-with-Directory-Trees.html#Working-with-Directory-Trees中指定的ftw函数,其回调函数如下所示:

/* Call-back of ftw function*/

int filter_mp3s(const char *dir_name, const struct stat *status, int typeflag){

    if (typeflag == FTW_D){

        struct dirent **mp3list;
        int num_archives;
        int counter;

        num_archives = scandir (dir_name, &mp3list, select_mp3_ext, alphasort);

        /* print mp3 names */
        if (num_archives > 0) for (counter = 0; counter <= num_archives - 1; counter++) printf("%s\n", mp3list[counter]->d_name);

    }

    return 0;
}

我真正想要做的是将文件的名称放入GTK组合框小部件中。问题是,该函数返回一个int类型,并且函数的参数不灵活,所以我可以&#34; save&#34;在某些条目中。换句话说,找到了mp3,但我不知道如何保存结果以便在其他功能的组合框中加载它们。我不想使用全局变量...... 我是新手,感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

如果回调没有客户参数(通常是void *),那么您将不得不将找到的数据放入全局变量中,这是不幸的。 如果这是一个问题(即您处于多线程环境中),则必须使用opendir接口实现自己的递归目录遍历版本。这并不难。