Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2007
    Posts
    50
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help needed with hex dump

    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
    ------



    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 Attached Files

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Yeah because the first character in that text file is a space which has a ASCII code of 0x20.

    fread doesn't read hex dumps (well at least not automagically like I think you might think it works) it starts reading that file character by character and dumping it into your struct and the first character is a space.
    OracleGuy

  • #3
    New Coder
    Join Date
    May 2007
    Posts
    50
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oracleguy View Post
    Yeah because the first character in that text file is a space which has a ASCII code of 0x20.
    Well, if I remove the space at the beginning of the file, I still get an error:


  • #4
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    I don't understand completely what you're trying to do, nor do I remember my C that well, but I played with your code and can offer some observations.

    oracleguy seems to be correct regarding the spaces at the beginning of each line. I deleted all the whitespace at the beginning of the file, but of course got the error that you posted in your second post.

    Then I noticed that your Header.Version is declared as a char. fread is going to read the first character in the file and put it in the version variable. Looking at the file you'll notice that the first character is 0 (again after removing whitespace). So you'll have to come up with a way to convert the starting 03 to just a single char which removes the 0 and leaves you with 3.

    So, I removed the starting 0 in the text file and ran the code again, but it still didn't work...Then I noticed that your if statement is trying to compare header.version (a char) with 3 (an int). The short answer is that this won't work based on how you have things setup. So I changed the if condition to

    if ( header.version != '3' ) /* what about 0x83 */

    Running the code once more after this change at least got it passed the if condition. But the date surely isn't correct, I assume the same issue of reading single chars from above continues to apply to the rest of the information in the header struct.

    To sum up, to get your code to at least make it past the if condition I;
    1) Stripped out all leading whitespace in the file
    2) Changed the initial 03 to 3
    3) Changed the if condition to if ( header.version != '3' ) /* what about 0x83 */

    In the end this may not really solve anything, but hopefully it will give you a better idea of why your code isn't working.

    Gox

  • #5
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Your code would work if you were reading the original binary file if that is a hex dump of the original file. However if you want to have your program read the hex dump, like Gox said you'll need to do some more parsing to convert the two digit hex numbers into a single byte that they represent.
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •