[Serialization] Can I load two object from one input file?
Hi all, Code like this: // Save std::ofstream ofs( "c.out" ); A testA1; B testB1; testA1.Save( ofs ); testB1.Save( ofs ); ofs.close(); // Load std::ifstream ifs( "c.out" ); A testA11; B testB11; testA11.Load( ifs ); testB11.Load( ifs ); The testA1 and testB1 can save successful, and the testA11 can load successful, but when loading testB11, it throw a exception "invalid_signature". I have trace into the code, I found when the testA11 loading, the "file_signature" is "serialization::archive", but when testB11 loading, the "file_signature" is empty. Why it like this? What should I do? May be there is the same question before I ask, but I don't know what keyword could be search, sorry. Thanks!
On Thursday 27 December 2007 00:20:24 sjdf wrote:
// Save std::ofstream ofs( "c.out" );
A testA1; B testB1;
testA1.Save( ofs ); testB1.Save( ofs );
ofs.close();
// Load std::ifstream ifs( "c.out" );
A testA11; B testB11;
testA11.Load( ifs ); testB11.Load( ifs );
The simple solution would be to create a "C" type that contains an "A" and a "B". Then you could just save and load the C object. Hope This Helps, Justin
In my application, I am am saving 2 objects into the same archive and then reading them back correctly in the same order - so it is possible. Maybe you could post how you implemented "Load" and "Save" and I can try to help you. Mahesh
The program run ok when I compile it with mingw, but if I compile it
use vc71, the program will crash when excute "pbb->Load( ifs );".
Thinks!
code:
//----------------------------------------------------------------------------
#include <string>
#include <iostream>
#include <fstream>
#include
In my application, I am am saving 2 objects into the same archive and then reading them back correctly in the same order - so it is possible. Maybe you could post how you implemented "Load" and "Save" and I can try to help you.
Mahesh
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
#include <iostream>
#include <fstream>
#include
#include #include //---------------------------------------------------------------------------- using namespace std;
//---------------------------------------------------------------------------- class A { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & a; ar & b; }
public: int a; int b;
};
//---------------------------------------------------------------------------- class B { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & ms; ar & a; } public:
public: B( const string& s ) : ms( s ){} void print(){ cout << ms << endl; } private: B(){} // use by serialization only string ms; A a; };
//---------------------------------------------------------------------------- int main() { // Save A* pa = new A;
Rather than put your program on my debugger, here is the way I anticipated that the library would be used: pa->a = 3; pa->b = 5; B* pb = new B( "this is test B" ); { std::ofstream ofs( "all.out" ); boost::archive::text_oarchive oa(ofs); oa << pa; ob << pb; delete pa; delete pb; } A* paa; B* pbb { std::ifstream ifs( "all.out", std::ios::binary ); boost::archive::text_oarchive ia(ifs); ia >> paa; ia >> pbb; } cout << "A: " << paa->a << " " << paa->b << endl; pbb->print(); delete paa; delete pbb; return 0; } Robert Ramey
Thank you, Robert.
I really want to konw why the program can run successful when using
mingw to compile, and will crash when using vc71 to compile.
I have debug and trace into the code, I found when I execute
pbb->Load( ifs );, it will call this function:
basic_text_iarchive<Archive>::init(void){
// read signature in an archive version independent manner
std::string file_signature;
* this->This() >> file_signature;
if(file_signature != ARCHIVE_SIGNATURE())
boost::throw_exception(
archive_exception(archive_exception::invalid_signature)
);
the file_signature is empty, so throw the exception invalid_signature.
Can you tell me, why it like this?
2008/1/2, Robert Ramey
Rather than put your program on my debugger, here is the way I anticipated that the library would be used:
#include <iostream>
#include <fstream>
#include
#include #include //---------------------------------------------------------------------------- using namespace std;
//---------------------------------------------------------------------------- class A { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & a; ar & b; }
public: int a; int b;
};
//---------------------------------------------------------------------------- class B { private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & ms; ar & a; } public:
public: B( const string& s ) : ms( s ){} void print(){ cout << ms << endl; } private: B(){} // use by serialization only string ms; A a; };
//---------------------------------------------------------------------------- int main() { // Save A* pa = new A; pa->a = 3; pa->b = 5;
B* pb = new B( "this is test B" );
{ std::ofstream ofs( "all.out" ); boost::archive::text_oarchive oa(ofs);
oa << pa; ob << pb;
delete pa; delete pb; } A* paa; B* pbb { std::ifstream ifs( "all.out", std::ios::binary );
boost::archive::text_oarchive ia(ifs); ia >> paa; ia >> pbb; }
cout << "A: " << paa->a << " " << paa->b << endl; pbb->print();
delete paa; delete pbb; return 0; }
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
In your original example a) you' re reconstruction the archive for each object. I can't imagine what the purpose of this is. Certainly one would not expect to be able to serialize multiple objects to an archive if the archive is being re-constructed with every object. b) you're code contains things like ar << *this; This suggests to me that there is some confusion as to how the library is intended to be used. I would recommend a more careful revue of the demos and perhaps some of the tests which might be similar to your example. Robert Ramey
participants (4)
-
KSpam
-
Mahesh Venkitachalam
-
Robert Ramey
-
sjdf