I'm using the code below to read a struct from a binary file.
struct A
{
int s;
char cname[20];
};
int main () {
A mystruct;
ifstream is;
is.open ("filename.txt", ios::binary );
is.read ( &mystruct, sizeof(InpumystructtStruct) );
is.close();
return 0;
}
Suppose there are multiple structs in the binary file, of type struct A, struct B and struct C, with different size appearing in the file. If I want to read all of them in sequence, assuming I know the sequence, would this be correct?
struct A
{
int s;
char cname[20];
};
struct B
{
int s;
};
struct A
{
char cname[20];
};
int main () {
A mystructa;
B mystructb;
C mystructc;
ifstream is;
is.open ("filename.txt", ios::binary );
while( is.good() ) {
// determine struct to use
is.read ( &mystruct/*put right type*/, sizeof(/*put right tupe*/) );
}
is.close();
return 0;
}