This line:
file_write.write((char*)&structure_data,sizeOfStructure);
takes whatever structure_data
is, and just copies those blob of bytes that makes up structure_data
to a file.
It doesn't figure out what the members are. Also, this is the cause for thousands of SO questions that erroneously do coding like this, where structure_data
cannot be written to a file this way and have the file contents make sense. It is quickly discovered that the contents of the file are useless when an attempt to read back the data into a program
is unsuccessful.
Most of the time in those scenarios structure_data
would contain pointers, or members that are not C-layout compatible, i.e. non-POD types such as std::string
or std::vector
, that basically renders this technique of writing to a file like this totally useless (and invalid).
Look up object serialization such as this link on the topic
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…