Re: GList and combobox problem



On Fri, 19 Jan 2001 17:27:17 +0100, Marco Cova said:

> Hi all.
>  I have the following problem. 
>  I want to fill a GList *list with the names of files stored in a
>  directory and then use this list together with a combobox. So, I expect
>  to see the files' names as popdown strings, but all I get is some
>  garbage. A part of the code follows:
>  
>  /* function I use to initialize the list with files' names */
>  GList *init_list(GList **list, char *directory)
>  {
>   DIR *dp;
>   struct dirent *de;
>  
>   dp = opendir(directory);
>    while((de = readdir(dp)) != NULL)
>    {
>     *list = g_list_append(*list, de->d_name);

You need to g_strdup the de->d_name so *list = g_list_append(*list,
g_strdup(de->d_name));

otherwise you lose the pointers right after closing the dir.

>    }
>   
>    closedir(dp);
>    return(*list);
>  }
>  
>  
>  /* this is in main() */
>   GList *list;
>   GtkWidget *combo;  
>   int i;
>  
>   list = NULL;
>   list = init_list(&list, "/home/user/dir");
>   /* this correctly outputs files' names */
>   for(i = 0; i < g_list_length(list); i++) 
>    printf("DEBUG: list's element number %d is %s\n", i, g_list_nth(list,
>  i)->data);
>  
>   gtk_combo_set_popdown_strings(GTK_COMBO(combo), list);
>   gtk_widget_show(combo);
>  
>  I searched more info in the tutorial and the reference, but I couldn't
>  find a solution. Any help is much appreciated,
>  thanks,
>  Marco.
>  
>  
>  _______________________________________________
>  gtk-list mailing list
>  gtk-list gnome org
>  http://mail.gnome.org/mailman/listinfo/gtk-list
>  
>  

-- 
http://www.muhri.net -- muhri muhri net





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]