How to fill a Combo Box from a text file - GTK - C
<p>I'm having trouble filling my combo box from a text file I have. Let's say I have a text file of 10 lines (each line containing a word). Using my code, the combo box will end up having 10 items of the last line.</p>

<p>My actual text file is of this format:</p>

<pre><code>2p1 23 blocA 
2p2 16 blocB 
2p3 19 blocB 
</code></pre>

<p>Here is the initial code I used:</p>

<pre><code>    GtkWidget *create_class_combo ()
    {
       GList *glist = NULL;
       GtkWidget *combo;

       FILE *fp = NULL;
       Class c;

       combo = gtk_combo_new ();

       fp = fopen ("lists/class.txt","r");

       while (fscanf(fp, "%s %s %s \n", c.name, c.children, c.location) != EOF)
       {
           glist = g_list_append (glist, c.name);
       }

       fclose(fp);

       gtk_combo_set_popdown_strings (GTK_COMBO (combo), glist);

       return combo;
    }
</code></pre>

<p>This is the Class structure:</p>

<pre><code>    typedef struct Class
    {
       char name[20];
       char children[10];
       char location[30];
    } Class;
</code></pre>

<p>After some testing, it turns out that "g_list_append" will overwrite the last item it added with the new one, if you use the same variable as second parameter.</p>

<p>So, I used an array of strings. It didn't work. It shows me an empty combo box.</p>

<p>I used an array of a structure that contains only one string. It didn't work. It shows me an empty combo box.</p>

<p>I used g_list_concat alongside a second GList variable, while also initializing the first one at the end of each "while" cycle. It didn't work as well.</p>

<p>I'm just puzzled. 
Can you please help me?</p>

<p>Update: I found different functions for combo box and used them instead in this code. It works now.</p>

<pre><code>GtkWidget *create_class_combo ()
{
GtkWidget *combo;

FILE *fp = NULL;
Class c;

combo = gtk_combo_box_text_new_with_entry ();

fp = fopen ("lists/class.txt","r");

while (fscanf(fp, "%s %s %s \n", c.name, c.children, c.location) == 3)
{
    gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), c.name);
}

fclose(fp);

return combo;
}
</code></pre>