Serialization and deserialization of two dimensional float array in C++ -
i have structure:
struct desc { int rows; int cols; }
and 2 dimensional float array.
i need transfer structure , data
through network. how serialize/deserialize correctly?
this now:
desc desc; desc.rows = 32; desc.cols = 1024; float data[rows][cols]; // setting values on array char buffer[sizeof(desc)+sizeof(float)*desc.rows*desc.probes]; memcpy(&buffer[0], &desc, sizeof(desc)); // copying struct buffer memcpy(&buffer[0]+sizeof(desc), &data, sizeof(float)*rows*probes); // copying data buffer
but i'm not sure if correct approach.
can give me hints how this?
if stay in c++ , want efficient use boost serialization - otherwise json might friend. adapted demo serializing struct file - writes/reads to/from streams.
#include <fstream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> struct desc { int rows; int cols; private: friend class boost::serialization::access; template<class archive> void serialize(archive & ar, const unsigned int version) { ar & rows; ar & cols; } public: desc() { rows=0; cols=0; }; }; int main() { std::ofstream ofs("filename"); // prepare dummy struct desc data; data.rows=11; data.cols=22; // save struct file { boost::archive::text_oarchive out_arch(ofs); out_arch << data; // archive , stream closed when destructors called } //...load struct file desc data2; { std::ifstream ifs("filename"); boost::archive::text_iarchive in_arch(ifs); in_arch >> data2; // archive , stream closed when destructors called } return 0; }
note: i've not checked if example works.
*jost
Comments
Post a Comment