You have to be more careful when you write binary data to file.
Given your main structure:
1 2 3 4 5 6
|
struct fileToDump {
subset* subsets;
int iNumSubsets;
int iVersionID;
};
|
There are only two concrete things in there:
iNumSubsets and
iVersionID, both integers. The other element is a
pointer to another structure, not the structure itself. Writing a pointer to file is meaningless. You want to write the
data that the pointer addresses. Hence, your write routine needs to be a bit more sophistocated.
Here is a simple example to help:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
struct point
{
int x, y;
};
struct points
{
unsigned count;
point* data;
};
void writePointToFile( std::ofstream& f, const point& p )
{
f.write( static_cast <char*> ( &p ), sizeof( point ) );
}
bool writePointsToFile( const char* filename, const points& pts )
{
std::ofstream f( filename, ios::binary );
f.write( static_cast <char*> ( &(pts.count) ), sizeof( unsigned ) );
for (unsigned n = 0; n < pts.count; n++)
{
writePointToFile( f, pts.data[ n ] );
}
return f.good();
}
|
Other considerations:
- endianness
- data sizes (is an
int two bytes or four? or eight?)
- floating point number format (may vary depending on hardware)
- you list the number of things in your structures
after the things being numbered.
This is a nightmarish thing to. Change it around and put the number of things
first
(like I did in the example above).
I have tried to use syntax that is easy to follow above, but it is possible to do things differently.
Hope this helps.