保存ftw函数的目录搜索结果

发布于 2024-12-11 19:38:06 字数 1038 浏览 0 评论 0原文

我要做的就是从确定的预先指定的目录及其子目录中递归获取“.mp3”档案。我在获取 mp3 并在控制台上打印它们时没有遇到任何问题。我正在使用 http://www.gnu.org/software/libc/manual/html_node/Working-with-Directory-Trees.html#Working-with-Directory-Trees,其回调函数看起来像这样:

/* 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 类型,并且该函数的参数不灵活,因此我可以“保存”条目。换句话说,找到了 mp3,但我不知道如何保留结果以便将它们加载到其他功能的组合框中。我不想使用全局变量... 我是新手,提前感谢您的帮助。

what I have to do is recursively get ".mp3" archives from a determined pre-specified directory and its subdirectories. I did not have a problem getting the mp3's and printing them on console. I am using the ftw function specified in http://www.gnu.org/software/libc/manual/html_node/Working-with-Directory-Trees.html#Working-with-Directory-Trees, its call-back function would look like this:

/* 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;
}

What I really want to do is put the names of the files into a GTK combo-box widget. Problem is, that function returns an int type and the function is not flexible with its parameters so I could "save" in something the entries. In other words, mp3's are found but I have no idea how I could keep the results in order to load them in the combo-box in other function. I do not want to use global variables...
I'm new into this, thanks in advance for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

强辩 2024-12-18 19:38:06

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

If the callback doesn't have a customer argument (usually a void*) which appears to be the case, you will have to put found data into a global variable, which is unfortunate.
If that's a problem (i.e. you are in a multithreaded environment) you will have to implement your own version of recursive directory traversal using opendir interface. It's not difficult.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文