gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:48:56 25 4
gpt4 key购买 nike

我正在尝试创建一个迭代程序,使用 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 来解决此问题的实现,我们将不胜感激。

最佳答案

我最终设法弄清楚了。

这是代码:

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

我希望这段代码将来能对某人有所帮助。

关于c - 使用 C 和 GSList 迭代文件夹读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18203497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com