buffer has extra contents in gtk+ 2.0

This is a discussion on buffer has extra contents in gtk+ 2.0 within the Linux Programming forums, part of the Platform Specific Boards category; I am trying to learn C and gtk+ 2.0 and I just wrote a simple program that puts a text ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    buffer has extra contents in gtk+ 2.0

    I am trying to learn C and gtk+ 2.0 and I just wrote a simple program that puts a text file to stdout when a button is pushed.

    For some reason, the output from the file leaves an extra line in the stdout buffer, such that if you press the button again, this extra line (composed of those wierd merge-sign like characters) appears before the file content (ie, it is the first line in the buffer). Obviously, if I __fpurge(stdout) first (or at the end of the last call), then everything is fine.

    However, a non-gtk version of the same thing does not leave this extra line and so doesn't require flushing. Does anyone know why? Note that if I use "g_print" instead of "printf" an extra line appears at the end of the output instead of being kept in the stdout buffer (it looks like a different line than the other one too).

    Also: is there not a portable, ANSI version of __fpurge? Or are you suppose to redirect fflush for unwanted stdout contents?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    You should probably show some small code sample illustrating the problem
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Wink some sample code

    The actual function does not require any gtk, so can be included in a non-gtk test program (removing "*widget" from the preliminary arguments):
    Code:
    #include <stdio_ext.h> //for __fpurge
    
    static void printfile (GtkWidget *widget, char *filename) {         
            int len=0;
            char *line = NULL;
            FILE *fst = fopen (filename, "r");
            printf ("\n-----%s-----\n", filename);
            while (feof(fst) == 0) {
                    line = getline(&line,&len,fst);
                    printf ("%s", line);
            }   
            fclose (fst);
            __fpurge(stdout);
    }
    Again, the mystery is an extra line (of garbage) produced when this function is called within gtk, otherwise __fpurge would be unnecessary.
    I am not really sure if my method of reading the file (using "while (feof(fst) == 0)") is a normal practice, and in the final iteration of the loop getline must contain an EOF to trip "feof", and the EOF is printed (except the EOF isn't even a space on the console) Here's another interesting element:
    • as already mentioned, if using g_print (from gtk.h) instead of printf, the garbage is actually printed instead of being left in the buffer, as if fflush were used in place of __fpurge. Since the g_print call is before the __fpurge call, printing the garbage can then not be avoided.
    • if you include feof's return in the printf (ie, printf("%d %s", feof(fst), line)), in a non-gtk test the output would be:
      0 data line 1
      0 data line 2
      0 data line 3
      1
    • if you do the same in a gtk context, the final "1" does not appear. If you use g_print, you get:
      0 data line 1
      0 data line 2
      0 data line 3
      1 garbage written over data line 3

    This is not really a big problem -- I was just hoping there would be someone intimately familiar with gtk2 programming who could tell me why this is. Such explanation might help me in solving future gtk riddles, should they arise...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    re: getline()

    nope, that's the GNU getline (this is the linux board, right?)

    altho in all honesty, i did not know there's a C++ getline, so maybe these are the same.

    However, probably they aren't because if i'm not using it right, i don't see how. Straight from the documentation:

    If you set *line to a null pointer, and *len to zero, before the call, then getline allocates the initial buffer for you by calling malloc. In either case, when getline returns, *line is a char * which points to the text of the line.

    And I haven't had any problems with it before. Really really I need a gtk guru here.
    Update: Actually from just glancing at your reference, no, obviously the GNU getline and the C++ getline are not the same.
    Last edited by MK27; 08-04-2008 at 11:37 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The problem has little to do with GTK and everything to do with teh FAQ that robwhit posted - you are USING data after you read EOF.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 04:46 PM
  2. Tetris Questions
    By KneeGrow in forum Game Programming
    Replies: 19
    Last Post: 10-28-2003, 10:12 PM
  3. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 04:15 AM
  4. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 05:21 PM
  5. getline problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2001, 09:28 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21