View Full Version : Help needed with hex dump

09-15-2008, 01:19 AM
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...:(

http://img244.imageshack.us/img244/4159/errorre5.th.jpg (http://img244.imageshack.us/my.php?image=errorre5.jpg)

#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:");

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

09-15-2008, 03:26 AM
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.

09-15-2008, 03:31 AM
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:

http://img57.imageshack.us/img57/4941/errorwy1.th.jpg (http://img57.imageshack.us/my.php?image=errorwy1.jpg)

09-15-2008, 07:18 AM
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.


09-15-2008, 03:19 PM
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.