reading multiple stl objets with serialization

I have saved multiple map values inside a file using boost serialization, now my problem is i don't know exactly how i can read all of those maps: Till now i've done these much;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <map>
#include <fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>

#include <boost/serialization/map.hpp>

int main(int argc,char** argv) {
  std::ofstream s("tmp.oarchive"); 
  std::map<int,int> m, m1, m2;
  m[1] = 100;
  m[2] = 200;
  m1[123]=3222; m2[1]=23;
  m1[1111]=232; m2[23]=1;
  {
     boost::archive::text_oarchive oa(s);
     oa << m; oa << m1; oa << m2;
  }

    std::map<int,int> n; std::map<int,int> n1;
    {
        std::ifstream s("tmp.oarchive");
        boost::archive::text_iarchive ia(s);
        if(s.is_open()) {
          ia >> n; 
          std::cout<<n[1]<<"\t"<<n[1111]<<std::endl;   
        }
    }

}

Here the number of map object can be very large, so is their any kind of iterator of function similar to fseek() to read the input archive like in normal text file.
Last edited on
So what's the problem in deserialising these multiple maps?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <map>
#include <fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/map.hpp>
#include <iostream>

int main() {

  const std::size_t N = 3 ;
  const std::string path = "tmp.oarchive" ;

  const std::map<int,int> maps_written[N] {
      { {1,100}, {2,200} }, { {123,3222}, {1,23} }, { {1111,232}, {23,1} }
  };

  {
     std::ofstream ostm(path);
     boost::archive::text_oarchive oarch(ostm);
     for( const auto& map : maps_written ) oarch << map ;
  }

  {
     std::map<int,int> maps_read[N] ;
     std::ifstream istm(path);
     boost::archive::text_iarchive oarch(istm);
     for( auto& map : maps_read ) oarch >> map ;

     for( std::size_t i = 0 ; i < N ; ++i )
        std::cout << i << ". " << ( maps_read[i] == maps_written[i] ? "ok\n" : "not ok\n" ) ;
  }
}

http://coliru.stacked-crooked.com/a/4e92d416f193c1fd
Topic archived. No new replies allowed.