Help needed with hex dump

Discussion in 'C' started by joeserhal, Sep 15, 2008.

  1. joeserhal

    joeserhal New Member

    Joined:
    Feb 13, 2008
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    Hey guys,

    I'm trying to write a program that would read a dbase III file as input and make some changes according to the need of the user (delete a record, delete a field...). Anyways, what I am doing is the following:
    1) reading the dbase III file
    2) creating a hex dump out of it
    3) reading the hex dump and making changes to the hex dump

    now, what i need to do first is make sure that dump corresponds to one of dbase III file (the first byte represents the version of the dbase III file: it should be "03" )

    I have wrote a test program that should make such checkings but it doesn't seem to work...:(

    ERROR
    ------
    [​IMG]


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main (int argc, char *argv[])
    
    {
      
    FILE *fd;
    char ch;
    char filename[20];
      
    struct {
        char version;
        struct {
          char yy;
          char mm;
          char dd; } modify;
        int number_of_records   ; /* 32 bits */
        } header;
    
      
    printf ("sizeof header = %d\n", sizeof(header) );
    
    printf("Enter file name:");
    scanf("%s",filename); 
      
    fd = fopen ( filename, "rb");
    
       
    if ( fd == NULL )
      
        {
         printf ("Unable to open file %s\n", filename);
         system ("pause");
         exit (1);
      }
    
      
    /*  read the first part of the header information */
      /*  and verify that it is a dbase III file        */
    
      
    fread ( &header, 1, sizeof ( header ), fd );
    
      
    if ( header.version != 3 ) /* what about 0x83 */
      
        {
        printf ("not a dbase 3 file, found %x\n", header.version );
        system ("pause");
        exit (2);
      }
    
      
    printf ("Date last updated:  %2.2d/%2.2d/%2.2d\n",header.modify.mm, header.modify.dd, header.modify.yy );
    system ("pause");
      
    fclose ( fd );
    system ("pause");
      
    return 0;
    
    
    } /*  end of main */
    

    I have attached a sample hex dump of a dbase III file.

    Anybody knows why this is not working???
     

    Attached Files:

  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    You have size and count the wrong way round; you're asking fread to read in sizeof(header) records each of which is 1 byte long.
     
  3. ljlong

    ljlong New Member

    Joined:
    Dec 30, 2008
    Messages:
    10
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software engineer, embedded, rtos, machine control
    Location:
    Santa Rosa, CA
    My guess would be that you are having a problem with how the structure is packed in memory. Memory is being allocated for the structure that isn't one to one with the binary read.
    You need to do reads of a size you control and then put them into the structure. There are compiler params you can use to pack the structure but I've had limited success with that approach.

    jim
     
  4. ljlong

    ljlong New Member

    Joined:
    Dec 30, 2008
    Messages:
    10
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software engineer, embedded, rtos, machine control
    Location:
    Santa Rosa, CA
    oops, I should have checked the arguments in the function call... dumb on my part...
    also, for some reason I didn't see the previous reply?
    jim
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice