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.

pigelet

Programmer
Dec 13, 2003
4
US
hello: am working on a program that read through a binary data file. the problem that I am having right now is this. I have 6 different types of binary data files (different formats, - hence defined 6 different structures). the way that I did is was to make 6 different functions which call fread with the associated structure then I do my processing there. I was wondering if someone could tell me (if it is possible a way to be able to change the associtated struct so that I only have one function which calls fread. As in while (fread(<variable struct>, size, # items, file *ptr).

thxs for the help
 
The fread() isn't the problem, it's what happens after the read which still has to be coded for each record variant. The fread() in each of these functions is but a single line.

All you're going to end up doing is some variation of changing
Code:
while (fread(<variable struct>, size, # items, file *ptr)
repeated in 6 different functions reading records of the correct size

With something like this
Code:
size_t size = /* choose appropriate size */
while (fread(<variable struct>, size, # items, file *ptr) ) {
    /* choose appropriate function to handle record */
}

If you really want to get creative, then you can start using function pointers to indicate which function to call.

--
 
hello Salem & thxs u so much for providing some insight.
my question is still on setting up the fread.
here is what I had in mind to do

define struct a; struct b; etc
have some function say process(<struct>) where struct could be either of type a, b, etc
then within this function would be a loop

while (fread<the passed in struct> >0)
then do......

but I do see ur point... so I should probably just go with having multiple functions then?

thxs
 
> so I should probably just go with having multiple functions then?
Well it's probably the most direct answer - its obvious what is going on to anyone reading the code.


Use this only if you really understand what is going on. There is an extra layer of obscurity added just so you can move a single line of code outside of each of your functions.
Code:
#include <stdio.h>

struct foo {
    int a;
};
struct bar {
    char    name[10];
};
union baz {
    struct foo  t1;
    struct bar  t2;
};

void process ( FILE *fp, size_t size, void (*fn)(union baz*) );
void process_foo ( union baz *ptr );
void process_bar ( union baz *ptr );

int main ( ) {
    FILE *fp = fopen( &quot;file&quot;, &quot;rb&quot; );
    process ( fp, sizeof(struct foo), process_foo );
    return 0;
}

void process ( FILE *fp, size_t size, void (*fn)(union baz*) ) {
    union baz input;
    while ( fread( &input, size, 1, fp ) == 1 ) {
        fn(&input);
    }
}

void process_foo ( union baz *ptr ) {
    printf( &quot;%d\n&quot;, ptr->t1.a );
}

void process_bar ( union baz *ptr ) {
    printf( &quot;%s\n&quot;, ptr->t2.name );
}

--
 
so am back again with a dif question
I figure out how to make it work

if you declare a character array and do an fread to it from a binary data file, I noticed that the only data that is copied over is the character stuff in my original data file.
the file Iwas worknig with had characters, double short etc in each record.
can someone tell me why? cus I was tinkin that as long the fread fucntion takes in a void * ptr

thxs piglet out
 
> I noticed that the only data that is copied over is the character stuff in my original data file.
The rest of it will be there, it will just look for the most part like random bytes.

If you have
Code:
struct foo {
    char name[10];
    int age;
};

Then you can write to a file using
Code:
struct foo myvar;
fwrite( &myvar, sizeof(myvar), 1, fp );

And read it again using
Code:
struct foo myvar;
fread( &myvar, sizeof(myvar), 1, fp );

You can also read it into a char array. All the data will be there, it just wont make as much sense.
Code:
unsigned char arr[sizeof(struct foo)];
fread( arr, sizeof(arr), 1, fp );

--
 
compliments of the season. thxs for the help Salem.


piglet
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top