- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个迭代程序,使用 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/
我正在处理类似的事情,但是它会抛出错误。 我只是将数组放入一个变量中并以这种方式传递它,但我正在查看近 500 行,例如无模式数据的行。 (所以我不能使用循环) 此外,使用 GSList 的全部目的是
你好, 我正在尝试使用 glib.h 中的 GSList,但在用 char * 元素填充列表时遇到问题。 代码如下: GSList * res = NULL; char * nombre; while
我正在尝试创建一个迭代程序,使用 GSList 和 C 从特定的起始文件夹读取所有文件夹。目前我还没有找到代码中的缺陷。 我遇到的问题是它读取每个文件夹及其所有子文件夹,直到到达包含更多子文件夹的文件
将 GSList 传递给 Glib 宏(例如 G_OBJECT_TYPE_NAME()、GTK_IS_WIDGET() 等)会导致段错误。 这是一个问题,因为我的程序必须处理 GObjects 列表,
给出代码片段: #include #include void print_city(gpointer value, gpointer data) { printf("%s, ", valu
如何在 DBusInterface 中创建方法对于 purple_find_buddies ?具体来说,我必须指定什么作为返回类型而不是 GSList*? 最佳答案 看起来,它只是变成了 List -
我想放一个GSList在 GHashTable 里面,这是我管理所有这些的方式: #include #include typedef struct Foo_ { GHashTable * ba
我正在尝试使用函数 g_slist_append(list, &structure) 将结构添加到单向链表。这似乎可行(它正在添加指针),但是我似乎无法在读取链表时找到查看结构中元素的方法。 我的结构
我正在更新一些 1990 年代的 C 代码并将其转换为 C++。原始代码在很大程度上依赖于 Gnome's gslist (单链表)。我发现 gslist 很难集成到我的面向对象代码中,所以我真的很想
我是一名优秀的程序员,十分优秀!