Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

binary data files & structures 1

Status
Not open for further replies.

littlepiglet

Programmer
Mar 2, 2004
5
US
Howdi and know how to do this the rudimentary way but woul like some ideas on more refined ways to doing this
I get some data from a database and read it into a binary data file with each "record" being of size 900 bytes.
Now each record translate out to about 70 data fields.. I mean record A could contain priceA (type float), priceB (trice int), priceC (type double), TkrName( type char) etc..
What I want to do is COMPare two records from two different files and return to a user if they are any differences and where exactly the differences lie/are


the way Iw as coding this was writing out for each record all the data fields to a file and then doing the same for the other file and using the unix diff utility but this is kind of basis, thanks for the help
piglet
 
If you're dealing with two binary files, then you can use

- fread() to read a binary record from each file
- memcmp() to compare two binary records.

At that level, you can easily tell whether two records are the same or not without knowing what the structure of the records is (only the length of the records is required).
So you can say "record 33 is different".

If you then make your program aware of the structure, then you can refine your tests to compare each field of a record according to its type.
So you can say "record 33 field 4 is different"

--
 
Hey Salem: thxs for responding. I understand the bit about reading a record from each file and using memcmp on it but am not clear as to the testing afterwards.
Are you saying that regardless I am going to have to compare each field and then return a noted difference ?

thxs
 
Yes, at some point you will need to overlay some kind of structure on those records.

How many different types of record are you dealing with?


--
 
Morning:
I am going to be dealing with just one type of record...

I dont know how to do this but could I use some kind of bit masking so that at the end I could tell what "field" was changed/different?
thxs
 
Here's one idea for you
Code:
#include <stdio.h>
#include <string.h>

/* the different data types supported */
typedef enum data_types {
    is_int,
    is_float,
    is_string
} data_types;

/* a description for each field */
typedef struct field {
    char        *field_name;    /* its name */
    data_types  type;           /* what data type */
    int         position;       /* position within a record */
    int         length;         /* number of bytes it uses */
} field;

/* example to match the dummy data which follows */
field fields[] = {
    { "name", is_string, 0, 16 },
    { "age",  is_int,    16, 4 },
};

/* compare function */
void compare ( void *r1, void *r2 ) {
    unsigned char *rec1 = r1;
    unsigned char *rec2 = r2;
    int i;
    int len = sizeof(fields)/sizeof(fields[0]);
    int diff;

    for ( i = 0 ; i < len ; i++ ) {
        switch ( fields[i].type ) {
            case is_int:
            {
                int *pa = (int*)rec1, *pb = (int*)rec2;
                diff = *pa != *pb;
            }
            break;
            case is_float:
            {
                float *pa = (float*)rec1, *pb = (float*)rec2;
                diff = *pa != *pb;
            }
            break;
            case is_string:
            {
                char *pa = (char*)rec1, *pb = (char*)rec2;
                diff = strncmp( pa, pb, fields[i].length );
            }
            break;
        }
        if ( diff ) {
            printf( "Difference in field %s found\n", fields[i].field_name );
        }
        rec1 += fields[i].length;
        rec2 += fields[i].length;
    }
}

/* dummy just to create some data */
typedef struct {
    char    name[16];
    int     age;
} test;
int main ( ) {
    /* example data - use fread() to get these from a pair of files */
    test    t1 = { "fred", 22 };
    test    t2 = { "fred", 33 };
    compare( &t1, &t2 );
    return 0;
}

--
 
Hello SaleM: sorry it took me so long to respond. I appreciate the help and trudging along with my project.
I have a side question for u.. how did u become so good @ coding

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top