I have the following struct
struct MyStruct
{
int param1;
float param2;
double param3;
}
which I can write to a binary file using
fstream binary_file(file, ios::out|ios::binary);
binary_file.seekg(0, ios::beg);
binary_file.write((char *)aStruct,sizeof(MyStruct));
binary_file.close();
and I can recover it using
ifstream binary_file;
binary_file.open(file, ios::binary);
binary_file.seekg(0, ios::beg);
binary_file.read((char *)aStruct, sizeof(MyStruct));
binary_file.seekg (0, ios::end);
binary_file.close();
This all works fine. Now change the defintion of the struct to
struct MyStruct
{
int param1;
float param2;
double param3;
int paramA;
float paramB;
double paramC
}
The question is, if I read a file which was written before the definition change, will param1, param2 and param3 always be correctly set and can I be sure that paramA, paramB and paramC will not be assigned any junk? Parameters will only be added to the end of the struct.
According to the reference the ifstream read function should stop if eof is encountered before having read the specified number of bits, so hopefully this is as easy as it sounds. My tests also indicate that the answer to the question would be yes, however I want to make sure with you guys as I have been reading about for example padding in binary files and don't completely understand how that works.