Dear list I try to read back a serialized structure (binary). It works fine with gcc 3.3 on linux but I get problems with visualstudio 7.1. reading back the file.
wrong input data uncaught exception at: ar >> new_strucutre;
Are there some hints/ experiences with Visualstudio ?? Below I have a short code abstraction what I'm doing basically. Thanks luke ==== snip ==== struct foo { int posx; int posz; friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & posx; ar & posz; } }; class Manager { public: Manager(); ~Manager(); std::vector<foo> fool_; friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & fool_; } private: }; struct test { int id; int age; Manager manager; friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & id; ar & age; ar & manager; } }; int main() { //CREATE test test_foo; test_foo.id = 1; test_foo.age = 2; foo new_foo; new_foo.posx = 100; new_foo.posz = 110; test_foo.manager.fool_.push_back(new_foo); //WRITE std::ofstream ofsb("filenameb"); boost::archive::binary_oarchive ob(ofsb); ob << test_foo; ofsb.close(); //READBACK std::ifstream ifs(fname.c_str(), std::ios::binary); boost::archive::text_iarchive ia(ifs); ia >> new_test; // EXCEPTION ifs.close(); } ==============
Note that native binary archives are not guarenteed portable across environments. text and xml archives should be. Also note that there is demo of a portable binary archive (it doesn't handle floats) included with the package. On the other hand, I would expect gcc 3.3 and visual studio 7.1 to produce binary archives which inter-operate - assuming they are both for x86 machines. It would be interesting to know where the difference comes from. Robert Ramey luke wrote:
Dear list
I try to read back a serialized structure (binary). It works fine with gcc 3.3 on linux but I get problems with visualstudio 7.1. reading back the file.
wrong input data uncaught exception at: ar >> new_strucutre;
Are there some hints/ experiences with Visualstudio ?? Below I have a short code abstraction what I'm doing basically.
Thanks luke
==== snip ====
struct foo { int posx; int posz;
friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & posx; ar & posz; } };
class Manager { public: Manager(); ~Manager();
std::vector<foo> fool_;
friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & fool_; }
private:
};
struct test { int id; int age; Manager manager;
friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & id; ar & age; ar & manager; }
};
int main() { //CREATE test test_foo; test_foo.id = 1; test_foo.age = 2; foo new_foo; new_foo.posx = 100; new_foo.posz = 110; test_foo.manager.fool_.push_back(new_foo);
//WRITE std::ofstream ofsb("filenameb"); boost::archive::binary_oarchive ob(ofsb); ob << test_foo; ofsb.close();
//READBACK std::ifstream ifs(fname.c_str(), std::ios::binary); boost::archive::text_iarchive ia(ifs);
ia >> new_test; // EXCEPTION
ifs.close();
}
==============
Hi, seems you're mixing binary and text? you're reading in text in your example...
//WRITE std::ofstream ofsb("filenameb"); boost::archive::binary_oarchive ob(ofsb);
//READBACK std::ifstream ifs(fname.c_str(), std::ios::binary); boost::archive::text_iarchive ia(ifs);
Also, I had to do
std::ofstream ofsb("filenameb", std::ios::binary);
before it would work correctly.
(I started using boost/serialization this week and
also had some issues at first (with vc7.1), but it's
working nicely now)
The serialisation lib is quite nice!
Filip Peters.
--- luke
Dear list
I try to read back a serialized structure (binary). It works fine with gcc 3.3 on linux but I get problems with visualstudio 7.1. reading back the file.
wrong input data uncaught exception at: ar >> new_strucutre;
Are there some hints/ experiences with Visualstudio ?? Below I have a short code abstraction what I'm doing basically.
Thanks luke
==== snip ====
struct foo { int posx; int posz;
friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & posx; ar & posz; } };
class Manager { public: Manager(); ~Manager();
std::vector<foo> fool_;
friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & fool_; }
private:
};
struct test { int id; int age; Manager manager;
friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & id; ar & age; ar & manager; }
};
int main() { //CREATE test test_foo; test_foo.id = 1; test_foo.age = 2; foo new_foo; new_foo.posx = 100; new_foo.posz = 110; test_foo.manager.fool_.push_back(new_foo);
//WRITE std::ofstream ofsb("filenameb"); boost::archive::binary_oarchive ob(ofsb); ob << test_foo; ofsb.close();
//READBACK std::ifstream ifs(fname.c_str(), std::ios::binary); boost::archive::text_iarchive ia(ifs);
ia >> new_test; // EXCEPTION
ifs.close();
}
==============
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
____________________________________________________ Sell on Yahoo! Auctions no fees. Bid on great items. http://auctions.yahoo.com/
Filip Peters wrote:
Hi,
seems you're mixing binary and text? you're reading in text in your example...
//WRITE std::ofstream ofsb("filenameb"); boost::archive::binary_oarchive ob(ofsb);
//READBACK std::ifstream ifs(fname.c_str(), std::ios::binary); boost::archive::text_iarchive ia(ifs);
Also, I had to do std::ofstream ofsb("filenameb", std::ios::binary); before it would work correctly.
(I started using boost/serialization this week and also had some issues at first (with vc7.1), but it's working nicely now) The serialisation lib is quite nice!
Filip Peters.
Hei ... thanks a lot std::ofstream ofsb("filenameb", std::ios::binary); ^^^^^^^^^^^^^^^^^ now I works for me too :-) luke
participants (3)
-
Filip Peters
-
luke
-
Robert Ramey