Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <fstream>
  2.  
  3. // include headers that implement a archive in simple text format
  4. #include <boost/archive/text_oarchive.hpp>
  5. #include <boost/archive/text_iarchive.hpp>
  6.  
  7. /////////////////////////////////////////////////////////////
  8. // gps coordinate
  9. //
  10. // illustrates serialization for a simple type
  11. //
  12. class gps_position
  13. {
  14. private:
  15.     friend class boost::serialization::access;
  16.     // When the class Archive corresponds to an output archive, the
  17.     // & operator is defined similar to <<.  Likewise, when the class Archive
  18.     // is a type of input archive the & operator is defined similar to >>.
  19.     template<class Archive>
  20.     void serialize(Archive & ar, const unsigned int version)
  21.     {
  22.         ar & degrees;
  23.         ar & minutes;
  24.         ar & seconds;
  25.        
  26.         ++degrees;
  27.         ++minutes;
  28.         ++seconds;
  29.     }
  30. public:
  31.     int degrees;
  32.     int minutes;
  33.     float seconds;
  34.     gps_position(){};
  35.     gps_position(int d, int m, float s) :
  36.         degrees(d), minutes(m), seconds(s)
  37.     {}
  38. };
  39.  
  40. int main() {
  41.     // create and open a character archive for output
  42.     std::ofstream ofs("filename");
  43.  
  44.     // create class instance
  45.     const gps_position g(35, 59, 24.567f);
  46.    
  47.     // write the content of object
  48.     std::cout << g.degrees << " " << g.minutes << " " << g.seconds << std::endl;
  49.  
  50.     boost::archive::text_oarchive oa(ofs);
  51.     // write class instance to archive
  52.     oa << g;
  53.  
  54.     // write the content of object
  55.     std::cout << g.degrees << " " << g.minutes << " " << g.seconds << std::endl;
  56.  
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement