Confused

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kevin C.

    Confused

    Can someone explain why the file output produces all zeros? It seems to work
    fine in memory (e.g. passing char pointers to printf) but when I output the
    file, it comes out as zeros. Bookkeeping seems correct (counts, sizes, etc.)
    and the output file is the correct size, just all zeros.


    cd_entry *src = (cd_entry*)(tpd + tpd->cd_offset);
    cd_entry *dest = (cd_entry*)(cur + cur->cd_offset);
    for(int i = 0; i < tpd->num_columns; i++)
    {
    *dest = *src;
    dest += sizeof(cd_entry );
    src += sizeof(cd_entry );
    }

    newlist->num_tables++ ;
    free(g_tpd_list ); // deallocate old memory
    g_tpd_list = newlist;


    //output to file
    FILE *fhandle = fopen("dbfile.b in", "wbc");

    if(fhandle == NULL)
    return FILE_OPEN_ERROR ;

    fwrite(g_tpd_li st, g_tpd_list->list_size, 1, fhandle);
    fflush(fhandle) ;
    fclose(fhandle) ;


  • Jens.Toerring@physik.fu-berlin.de

    #2
    Re: Confused

    In comp.lang.c Kevin C. <nomail@fake.co m> wrote:[color=blue]
    > Can someone explain why the file output produces all zeros? It seems to work
    > fine in memory (e.g. passing char pointers to printf) but when I output the
    > file, it comes out as zeros. Bookkeeping seems correct (counts, sizes, etc.)
    > and the output file is the correct size, just all zeros.[/color]
    [color=blue]
    > cd_entry *src = (cd_entry*)(tpd + tpd->cd_offset);
    > cd_entry *dest = (cd_entry*)(cur + cur->cd_offset);
    > for(int i = 0; i < tpd->num_columns; i++)
    > {
    > *dest = *src;
    > dest += sizeof(cd_entry );
    > src += sizeof(cd_entry );
    > }[/color]

    This looks as if you're trying to copy an array of structures, where
    'src' points to the first structure of the source array and 'dest'
    to the start of the destination array. If this is the case and
    sizeof(cd_entry ) isn't 1 by accident then this looks rather wrong,
    you would need

    for ( int i = 0; i < tpd->num_columns; src++, dest++, i++ )
    *dest = *src;

    On the other hand, using e.g. memcpy() might be faster then using a
    loop in that case...
    [color=blue]
    > newlist->num_tables++ ;
    > free(g_tpd_list ); // deallocate old memory
    > g_tpd_list = newlist;[/color]

    [color=blue]
    > //output to file
    > FILE *fhandle = fopen("dbfile.b in", "wbc");[/color]
    [color=blue]
    > if(fhandle == NULL)
    > return FILE_OPEN_ERROR ;[/color]
    [color=blue]
    > fwrite(g_tpd_li st, g_tpd_list->list_size, 1, fhandle);
    > fflush(fhandle) ;
    > fclose(fhandle) ;[/color]

    I am also confused. But I am confused by your description of the
    problem. I don't see a single char pointer which you could use
    with printf() in that snippet of yours - all there is is some
    'cd_entry' pointer, which you don't care to tell us what it is.
    You're operating with a variable 'newlist' that's not defined
    anywhere and has no recognizable type (but it is to be assumed
    that it's a pointer to some structure, but that's pure guessword).
    The first 8 lines don't seem to have any relevance to what you're
    doing afterwards.

    So you better try again and post the relevant parts of your program,
    including typedefs etc. instead something that looks like the
    result of a more or less random cut-and-paste operation.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
    \______________ ____________ http://www.toerring.de

    Comment

    • kyle york

      #3
      Re: Confused

      Greetings,

      Kevin C. wrote:[color=blue]
      > Can someone explain why the file output produces all zeros? It seems to work
      > fine in memory (e.g. passing char pointers to printf) but when I output the
      > file, it comes out as zeros. Bookkeeping seems correct (counts, sizes, etc.)
      > and the output file is the correct size, just all zeros.
      >
      >
      > cd_entry *src = (cd_entry*)(tpd + tpd->cd_offset);
      > cd_entry *dest = (cd_entry*)(cur + cur->cd_offset);
      > for(int i = 0; i < tpd->num_columns; i++)
      > {
      > *dest = *src;
      > dest += sizeof(cd_entry );
      > src += sizeof(cd_entry );[/color]

      Chnage the last two lines to:

      dest ++
      src ++

      which will increment one element. Currently, you're incrementing
      sizeof(cd_entry ) *elements* (not chars) at a time.

      [color=blue]
      > }
      >
      > newlist->num_tables++ ;
      > free(g_tpd_list ); // deallocate old memory
      > g_tpd_list = newlist;
      >
      >
      > //output to file
      > FILE *fhandle = fopen("dbfile.b in", "wbc");
      >
      > if(fhandle == NULL)
      > return FILE_OPEN_ERROR ;
      >
      > fwrite(g_tpd_li st, g_tpd_list->list_size, 1, fhandle);
      > fflush(fhandle) ;
      > fclose(fhandle) ;
      >
      >[/color]


      --
      Kyle A. York
      Sr. Subordinate Grunt
      DSBU


      Comment

      Working...