Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Help needed with hex dump (http://www.go4expert.com/forums/help-hex-dump-t13877/)

joeserhal 15Sep2008 05:58

Help needed with hex dump
 
1 Attachment(s)
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
------
http://img244.imageshack.us/img244/4159/errorre5.th.jpg


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

xpi0t0s 15Sep2008 18:41

Re: Help needed with hex dump
 
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.

ljlong 1May2009 06:34

Re: Help needed with hex dump
 
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

ljlong 1May2009 07:36

Re: Help needed with hex dump
 
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


All times are GMT +5.5. The time now is 01:04.