Help needed with hex dump

joeserhal's Avatar, Join Date: Feb 2008
Go4Expert Member
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
File Type: txt fruit.txt (1.2 KB, 6 views)
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.
0
ljlong's Avatar
Go4Expert Member
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
0
ljlong's Avatar
Go4Expert Member
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