I would like to write a struct to a file as binary. The struct has two members, one is POD-only but the problem is the second member is a string:
struct ToWrite
{
std::string _str;
PODType _pod;
};
If I was simply writing the POD type as binary I would do:
file.write((char*)&_pod, sizeof(_pod));
and to read back:
const PODType& pod = *reinterpret_cast<const PODType*>(&bytes[position]);
However, I'm aware the string is more-complicated because you need to record the size. If I were to add a third class member, being an int containing the size of the string, how do you write and read the struct?