0
votes

Hi guys I read a binary file which consists of 1000 records of unknown C structure. Structure has these data types char (1 byte) int (4 bytes) long long int (8 bytes) float (4 bytes) and double (8 bytes) and at the end of each record there is a double variable which keeps average of all other data fields in that record. Maximum data field in the structure can be 11 (including average field). for example

struct data{
char v1;
int v2;
float v3;
double v4;
long long int v5;
int v6;

double avg;
}

I could not find a way to check the type of the variable which I read from binary file.Only thing I need is a hint just can you tell me a way how to find data type.

1
The file should have a known format that tells you the size and type of each record. Without that, there's no way to know what's there. - dbush
Data in a binary file has no type. Once you read it into a variable in your program, that variable has whatever type you defined it to be. - Keith Thompson
But my professor keep saying this is correct, you can find a way :) I don't have any idea... - mr.Nobody

1 Answers

0
votes

In one sense there is no answer. You can construct data which looks like it is in one format whilst actually being in a different format. In reality, you can almost always "eyeball" it. Print the data as a stream of double,s float, integers of various width, and one sequence will look sensible and like real data.

But you're helped out by the knowledge that the last member is a double and also a mean. So simply load the data into a memory buffer, cast to int *, float * etc, add up the total, work out the mean, and check.