glib - Iterative folder read using C and GSList -


i'm trying create iterative program reads folders specific starting folder using gslist , c. haven't managed find flaw in code now.

the problem i'm having reads each folder , of it's subfolders until in reaches 1 more subfolders. after repeats open 1 directory.

the running result below: http://pastebin.com/jzmfbrxc

code:

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

any solve more appreciated. also, if have other implementation problem using c, sharing more appreciated.

i've managed figure out eventually.

here code:

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

i hope piece of code in future.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -