可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am having problems trying to serialise a vector (std::vector) into a binary format and then correctly deserialise it and be able to read the data. This is my first time using a binary format (I was using ASCII but that has become too hard to use now) so I am starting simple with just a vector of ints.
Whenever I read the data back the vector always has the right length but the data is either 0, undefined or random.
class Example { public: std::vector 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 am doing wrong or what to do or be able to point me in the direction that I need to do?
回答1:
You can't unserialise a non-POD class by overwriting an existing instance as you seem to be trying to do - you need to give the class a constructor that reads the data from the stream and constructs a new instance of the class with it.
In outline, given something like this:
class A { A(); A( istream & is ); void serialise( ostream & os ); vector v; };
then serialise() would write the length of the vector followed by the vector contents. The constructor would read the vector length, resize the vector using the length, then read the vector contents:
void A :: serialise( ostream & os ) { size_t vsize = v.size(); os.write((char*)&vsize, sizeof(vsize)); os.write((char*)&v[0], vsize * sizeof(int) ); } A :: A( istream & is ) { size_t vsize; is.read((char*)&vsize, sizeof(vsize)); v.resize( vsize ); is.read((char*)&v[0], vsize * sizeof(int)); }
回答2:
You're using the address of the vector. What you need/want is the address of the data being held by the vector. Writing, for example, would be something like:
size = example.size(); file.write((char *)&size, sizeof(size)); file.write((char *)&example[0], sizeof(example[0] * size));
回答3:
I would write in network byte order to ensure file can be written&read on any platform. So:
#include #include #include #include #include int main(void) { std::vector