使用C和GSList读取迭代文件夹

时间:2013-08-13 07:51:05

标签: c glib iteration

我正在尝试创建一个迭代程序,使用GSList和C从特定的起始文件夹中读取所有文件夹。我还没有设法找到我的代码中的缺陷。

我遇到的问题是它会读取每个文件夹及其所有子文件夹,直到达到一个包含更多子文件夹的文件夹。之后,它只重复打开一个目录。

运行结果如下: http://pastebin.com/jZMFBrxC

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <limits.h>
#include <errno.h>
#include <sys/types.h>

int main(int argc, char *argv[]) {
    GSList *list = NULL;
    list = g_slist_prepend(list, "/home/ravior/Documente");        /* Folder for searching */

    DIR *d;

    int index = 0;

    while((char *)g_slist_nth_data(list, 0) != NULL) {
        gchar *element = g_strdup((char *)g_slist_nth_data(list, 0));
        d = opendir(element);
        if(!d) {
            fprintf(stderr, "Couldn't open '%s' : %s\n", (char *)g_slist_nth_data(list, 0), strerror(errno));
            exit(EXIT_FAILURE);
        }

        printf("\n\nThe opened folder is: %s\n\n", (char *)g_slist_nth_data(list, 0));

        while(TRUE) {
            struct dirent *entry;
            const char *d_name;

            entry = readdir(d);
            if(!entry) {
                break;
            }

            d_name = entry->d_name;

            /* Some code here... */

            if(entry->d_type & DT_DIR && strcmp(d_name, "..") != 0 && strcmp(d_name, ".") != 0) {
                int path_length;
                static char path[PATH_MAX];

                path_length = snprintf(path, PATH_MAX, "%s/%s",element, d_name);
                if(path_length >= PATH_MAX) {
                    fprintf(stderr, "Path length has got too long.\n");
                    exit(EXIT_FAILURE);
                }

                printf("%s\n", path);
                list = g_slist_append(list, path);
                index++;
                printf("The appended element is: %s\n", (char *)g_slist_nth_data(list, index));
            }
        }

        if(closedir(d)){
            fprintf(stderr, "Couldn't close' '%s': %s\n",(char *)g_slist_nth_data(list, 0), strerror(errno));
        }

        list = g_slist_remove(list, (char *)g_slist_nth_data(list, 0));
        free(element);
        element = NULL;
        index--;
    }

    g_slist_free(list);

    return EXIT_SUCCESS;
}

任何帮助解决这个问题都不仅仅是值得赞赏的。此外,如果您使用C对此问题进行任何其他实现,则将非常感谢分享它。

1 个答案:

答案 0 :(得分:0)

我最终成功解决了这个问题。

以下是代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <limits.h>
#include <errno.h>
#include <sys/types.h>

int main(int argc, char *argv[]) {
    GSList *list = NULL;
    list = g_slist_prepend(list, "/home/ravior/Documente");        /* Folder for searching */

    DIR *d;

    int index = 0;

    while((char *)g_slist_nth_data(list, 0) != NULL) {

        gchar *element = g_strdup((char *)g_slist_nth_data(list, 0));
        d = opendir(element);
        if(!d) {
            fprintf(stderr, "Couldn't open '%s' : %s\n", element, strerror(errno));
            exit(EXIT_FAILURE);
        }

        printf("\n\nThe opened folder is: %s\n\n", element);

        while(TRUE) {
            struct dirent *entry;
            const char *d_name;

            entry = readdir(d);
            if(!entry) {
                break;
            }

            d_name = entry->d_name;

            /* Some code here... */

            if(entry->d_type & DT_DIR && strcmp(d_name, "..") != 0 && strcmp(d_name, ".") != 0) {
                int path_length;
                static char path[PATH_MAX];

                path_length = snprintf(path, PATH_MAX, "%s/%s",element, d_name);
                if(path_length >= PATH_MAX) {
                    fprintf(stderr, "Path length has got too long.\n");
                    exit(EXIT_FAILURE);
                }

                printf("%s\n", path);
                list = g_slist_append(list, strdup(path));
                index++;
                printf("The appended element is: %s\n", (char *)g_slist_nth_data(list, index));
            }
        }

        if(closedir(d)){
            fprintf(stderr, "Couldn't close' '%s': %s\n", element, strerror(errno));
        }

        list = g_slist_remove(list, (char *)g_slist_nth_data(list, 0));

        free(element);
        element = NULL;
        index--;
    }

    g_slist_free(list);

    return EXIT_SUCCESS;
}

我希望这段代码可以在将来帮助某人。