GList append problem



Hi there.

I have a problem with an application I wrote a few days ago. This is a
simple programm which has four combo boxes. The popdown boxes are filled
in with strings from a text file. The text file is simple, every line
contains a string terminated with a '\0' and a '\n'. The first string is a
name, the second is a price, the third is an id number, the fourth is a
comment. A record consists of this four strings, and a file can hold
unlimited number of this records, terminated with a 0xFF byte. When the
program starts, the corresponding strings from each record is read, and
appended to a GList. This GList will become the content of the popdown
box. The code with some comments:

---------------------------------------------------
#include <gtk/gtk.h>
#include <stdio.h>
#include "interface.h"
#include "support.h"

int main (int argc, char *argv[])
{
        GtkWidget *window1;
        GList *glist_kinek=NULL;
        GList *glist_mennyi=NULL;
        GList *glist_szamlaszam=NULL;
        GList *glist_kozlemeny=NULL;
        FILE *adat;
        int i=0, k=0;
        char buffer[80],buffer2[80],buffer4[80],buffer3[80],allbuff[]="    ";
          gtk_set_locale ();
     gtk_init (&argc, &argv);
  add_pixmap_directory (PACKAGE_DATA_DIR "/pixmaps");
    add_pixmap_directory (PACKAGE_SOURCE_DIR "/pixmaps");
    window1 = create_window1 ();
    adat=fopen("adatbazis","r");
 while(1)
/* This should run until the EOF character.
*/
  {
   glist_kinek=g_list_append(glist_kinek,fgets(buffer,80,adat));
/* This was the line, where we append the string to the glist
*/
          printf("%s",buffer);
/* We print out the appended string to stdout. This is for
verification purposes only.
*/
                  for (i=1;i<4;i++)
                          {       while (1)
                                  {
       if (fgetc(adat) == 0xa) break;
      }
     }
/* This code skips the next tree strings until the next NAME type
 string. The original code did not contain this. I know this is
 very ugly, but I tried to slow down the
 code with unnecessary loops, because I thought this is an 
 optimization bug. It looks like it isn't.
*/
/*      glist_mennyi=g_list_append(glist_mennyi,fgets(buffer2,80,adat));
 glist_szamlaszam=g_list_append(glist_szamlaszam,fgets(buffer3,80,adat));
 glist_kozlemeny=g_list_append(glist_kozlemeny,fgets(buffer4,80,adat));

 These are the other pop down boxes. They are commented out, until
 it turns out whats wrong.
*/
          if (fgetc(adat) == 255) break;
                  else fseek(adat,-1,SEEK_CUR);
/* End of file detection. Thats ugly too. Is for slowdown here.
*/
  }
    fclose(adat);
  gtk_combo_set_popdown_strings( GTK_COMBO(combo1), glist_kinek) ;
  gtk_combo_set_popdown_strings( GTK_COMBO(combo2), glist_mennyi) ;
  gtk_combo_set_popdown_strings( GTK_COMBO(combo3), glist_szamlaszam) ;
  gtk_combo_set_popdown_strings( GTK_COMBO(combo4), glist_kozlemeny) ;
  gtk_widget_show (window1);
  gtk_main ();
  return 0;
}

The output in the popdown box is:

OTP NYUGDIJ PENZTAR
OTP NYUGDIJ PENZTAR
OTP NYUGDIJ PENZTAR
OTP NYUGDIJ PENZTAR
OTP NYUGDIJ PENZTAR

Should be:

SZJA
EU HOZZAJARULAS
NYUGDIJ ALAP
EU ALAP
OTP NYUGDIJ PENZTAR

The difference is well visible. The last entry is repeated five times,
and the others are not shown. The read position from the file is good
because the printf shows the good results. The problem is with the GList I
think. With my knowledge, which comes mainly from The GTK Tutorial the
code should be OK, I can't imagine what could be the problem. This GLists
are double linked, is it possible that GLib updates the next/previous
pointers badly?

I tried to compile the code on two different computers. On a 1200 T-Bird,
gtk1.2.8,gcc2.95.2 and on a 266Celeron,gtk1.2.6, unknown gcc. I get the
same results.

Thanks for the help in advance.
Hello.
   Gyalokay Szabolcs 





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