I am having problems serializing a vector (std :: vector) in binary format, and then correctly deserialize it and can read the data. This is my first time using the binary format (I used ASCII, but now it has become too difficult to use), so I just start with a simple vector ints.
Whenever I read data back, the vector always has the desired length, but the data is 0, undefined or random.
class Example { public: std::vector<int> val; };
Write
Example example = Example(); example.val.push_back(10); size_t size = sizeof BinaryExample + (sizeof(int) * example.val.size()); std::fstream file ("Levels/example.sld", std::ios::out | std::ios::binary); if (file.is_open()) { file.seekg(0); file.write((char*)&example, size); file.close(); }
READ:
BinaryExample example = BinaryExample(); std::ifstream::pos_type size; std::ifstream file ("Levels/example.sld", std::ios::in | std::ios::binary | std::ios::ate); if (file.is_open()) { size = file.tellg(); file.seekg(0, std::ios::beg); file.read((char*)&example, size); file.close(); }
Does anyone know what I'm doing wrong or what to do or be able to point me in the direction that I need to do?
source share