Skip to content

Instantly share code, notes, and snippets.

@danhambleton
Created February 25, 2015 20:45
Show Gist options
  • Save danhambleton/ed0572af7ace373dc1ba to your computer and use it in GitHub Desktop.
Save danhambleton/ed0572af7ace373dc1ba to your computer and use it in GitHub Desktop.
Binary serialiation error
#include "stdafx.h"
#include <boost/serialization/vector.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <fstream>
class RawData {
private:
friend class boost::serialization::access;
template <class Archive> void serialize(Archive &ar, unsigned) {
ar & data;
}
public:
std::vector<double>data;
static void save(RawData rd, std::string path);
static bool load(RawData &rd, std::string path);
};
void RawData::save(RawData rd, std::string path) {
std::ofstream file(path);
if (file.good()) {
boost::archive::binary_oarchive oa(file, std::ios_base::binary);
//boost::archive::text_oarchive oa(file);
oa << rd;
}
file.flush();
file.close();
}
bool RawData::load(RawData &rd, std::string path) {
std::ifstream file(path);
if (file.good()) {
boost::archive::binary_iarchive ia(file, std::ios_base::binary);
//boost::archive::text_iarchive ia(file);
ia >> rd;
file.close();
return true;
} else
return false;
}
#include <iostream>
int main() {
std::string const path = "test.data";
{
// serialize
RawData old_data;
for(int i = 0; i < 10000; i++)
old_data.data.push_back((double)i);
RawData::save(old_data, path);
}
{
// deserialize
RawData new_data;
RawData::load(new_data, path);
// grab the chunks and test the values
for (int i = 0; i < new_data.data.size(); i++)
std::cout << new_data.data[i] << ", ";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment