Boost 1.45 Serialization : Getting archive exception while loading

Hi, I have changed the sample code given in the Boost site for Serialization and the saving of the file works fine. The code works fine without the switch case as it executes save and load in the same session. I can save and load in the same session. If I close the application and open the application again and try to load the saved(serialized) file, it is throwing an Archive Exception. I am trying this for a game that I am working on so as to save and load in different sessions. Code: ----------------------------------------------------------------------- #include <fstream> // include headers that implement a archive in simple text format #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/archive/binary_iarchive.hpp> #include <boost/archive/binary_oarchive.hpp> ///////////////////////////////////////////////////////////// // gps coordinate // // illustrates serialization for a simple type // class gps_position { private: friend class boost::serialization::access; // When the class Archive corresponds to an output archive, the // & operator is defined similar to <<. Likewise, when the class Archive // is a type of input archive the & operator is defined similar to >>. template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & degrees; ar & minutes; ar & seconds; } int degrees; int minutes; float seconds; public: gps_position(){}; gps_position(int d, int m, float s) : degrees(d), minutes(m), seconds(s) {} void gps_display() { std::cout << "\n Displaying....\n"; std::cout << degrees << " "<< minutes << " "<< seconds<<std::endl; } }; int main() { // create and open a character archive for output std::ofstream ofs("filename"); // create class instance const gps_position g(35, 59, 24.567f); gps_position newg; char ch; std::cout << "Enter an option\n 1. Save\n 2. Load\n 3. Display\n\n"; std::cin >> ch; switch(ch) { case '1': { // save data to archive boost::archive::text_oarchive oa(ofs); // write class instance to archive oa << g; // archive and stream closed when destructors are called } break; case '2': { // ... some time later restore the class instance to its orginal state // create and open an archive for input std::ifstream ifs("filename", std::ios::binary); boost::archive::text_iarchive ia(ifs); // read class state from archive ia >> newg; // archive and stream closed when destructors are called } break; case '3': newg.gps_display(); break; default: std::cout << "\nInvalid option\n"; } return 0; } ----------------------------------------------------------------------- Can we load a serialized file from the same program in a different session? -- Thanks & Regards ------------------------------ Tejaswin G Macharla -----------------------------

Tejaswin Macharla wrote:
Hi,
I have changed the sample code given in the Boost site for Serialization and the saving of the file works fine. The code works fine without the switch case as it executes save and load in the same session. I can save and load in the same session. If I close the application and open the application again and try to load the saved(serialized) file, it is throwing an Archive Exception.
I am trying this for a game that I am working on so as to save and load in different sessions.
This look OK to me. There is a pair of examples in the package. demo_save and demo_load. (also demo_xml_save and demo_xml_load). Try these examples. If they work - compare them to your own test. If they don't work, let me know. Robert Ramey

Hi, I have tried the xml examples and they work just fine. I figured out the problem in my program. Its a silly mistake. In the main() function, the ofstream creates the file "filename" and we save in the first attempt. In the second session, the ofstream again creates the file "filename" and it will overwrite the previous saved one and will be empty. so when I tried to load, it threw an Archive exception. I kept the std::ofstream ofs("filename"); in the case '1': block and it worked fine. As this problem is solved, how do we close this post? Thanks Robert. On Wed, Jan 12, 2011 at 10:13 PM, Robert Ramey <ramey@rrsd.com> wrote:
Tejaswin Macharla wrote:
Hi,
I have changed the sample code given in the Boost site for Serialization and the saving of the file works fine. The code works fine without the switch case as it executes save and load in the same session. I can save and load in the same session. If I close the application and open the application again and try to load the saved(serialized) file, it is throwing an Archive Exception.
I am trying this for a game that I am working on so as to save and load in different sessions.
This look OK to me.
There is a pair of examples in the package. demo_save and demo_load. (also demo_xml_save and demo_xml_load). Try these examples. If they work - compare them to your own test. If they don't work, let me know.
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Regards ------------------------------ Tejaswin G Macharla -----------------------------
participants (2)
-
Robert Ramey
-
Tejaswin Macharla