I have a problem using forced serialization using binary archives. It works when using a file stream, but I want to save it in my local variable and eventually save / load it to / from berkeley db. When I run the program, I get boost :: archive :: archive_exception: "stream error" when creating the binary_iarchive instance.
#include <sys/time.h>
#include <string>
#include <boost/serialization/serialization.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>
#include <sstream>
namespace boost {
namespace serialization {
template<class Archive>
void serialize(Archive & ar, timeval & t, const unsigned int version)
{
ar & t.tv_sec;
ar & t.tv_usec;
}
}
}
int main(int, char**)
{
timeval t1;
gettimeofday(&t1, NULL);
char buf[256];
std::stringstream os(std::ios_base::binary| std::ios_base::out| std::ios_base::in);
{
boost::archive::binary_oarchive oa(os, boost::archive::no_header);
oa << t1;
}
memcpy(buf, os.str().data(), os.str().length());
if(memcmp(buf, os.str().data(), os.str().length()) != 0)
printf("memcpy error\n");
timeval t2;
{
std::stringstream is(buf, std::ios_base::binary| std::ios_base::out| std::ios_base::in);
boost::archive::binary_iarchive ia(is, boost::archive::no_header);
ia >> t2;
}
printf("Old(%d.%d) vs New(%d.%d)\n", t1.tv_sec, t1.tv_usec, t2.tv_sec, t2.tv_usec);
return 0;
}
This works when initialization is done using os.str (), so I assume that my way of copying data to my buffer or to the wrong one.
source
share