memory mapped data not comming correctly

I have written a code where i am writing a map in a binary file and than reading it using boost memory mapping, but whenever i display the result it is not correct it only shows value as

1852795252

. what should i do? Here's my code:
#include <iostream>
#include <vector>
#include <utility>
#include <fstream>
#include <utility>
#include <fstream>
#include <iterator>
#include <string>
#include <boost/interprocess/file_mapping.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <iostream>
#include <fstream>
#include <boost/serialization/map.hpp>

using namespace boost::archive;
using namespace boost::interprocess;
void save()
{
{
std::ofstream file{"archive1.bin"};
text_oarchive oa{file};
std::map<int,int> m;
m[3] = 9;
oa << m;
}
}

void load()
{
file_mapping fm("archive1.bin", read_only);
mapped_region region(fm, read_only);
int * m = (int *)region.get_address();
std::cout<<m[3]<<std::endl;
}

int main()
{
save();
load();
}
Maybe because what you're doing makes no sense.

Despite the name, archive.bin isn't a raw dump of memory you can then just pretend is an array of integers.

I modified your cout to include std::hex in the format.
1
2
3
4
5
6
7
$ ./a.out 
6e6f6974
$ hd archive1.bin 
00000000  32 32 20 73 65 72 69 61  6c 69 7a 61 74 69 6f 6e  |22 serialization|
00000010  3a 3a 61 72 63 68 69 76  65 20 31 32 20 30 20 30  |::archive 12 0 0|
00000020  20 31 20 30 20 30 20 30  20 33 20 39 0a           | 1 0 0 0 3 9.|
0000002d

6e6f6974 is just the 4th quad inside the file, corresponding to the letters of "tion".

So what is the proper way to write the map values in that file so that i can read them using boost memory mapping ? And will it work for the key and value data type as string also?
Last edited on
> so that i can read them using boost serilization ?
Go and read the boost serialisation documentation to find out.

I mean, you managed to grok this.
1
2
std::ofstream file{"archive1.bin"};
text_oarchive oa{file};


So I'm guessing the reverse begins with
1
2
std::ifstream file{"archive1.bin"};
text_oarchive oa{file};


> And will it work for the key and value data type as string also?
Sure.
Actually my file is very big in the range of 1-10 gb cas as much the new objects are trained its pattern value will be keep on inserting in the file. So i was trying to read the file using text_oarchive outside the main function, but i'm getting errors saying that ia doesn't name a type in below program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <ros/ros.h>

#include <map>
#include <fstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/map.hpp>
#include <iostream>
using namespace boost::archive;

  std::ifstream file{"archive.txt"};
  text_iarchive ia{file};
  std::map<int,int> pobjr;
  ia >> pobjr;

int main (int argc, char** argv)
{
    return 0;
}

How can i read the file with boost archive such that, when i run this program even though the main function runs multiple times the file is read only once at starting and the data can be read instantly inside the main or any function?
*shrug*
What you posted won't compile either.

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
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <vector>
#include <utility>
#include <fstream>
#include <utility>
#include <fstream>
#include <iterator>
#include <string>
#include <boost/interprocess/file_mapping.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <iostream>
#include <fstream>
#include <boost/serialization/map.hpp>

using namespace boost::archive;
using namespace boost::interprocess;
void save()
{
    {
        std::ofstream file{"archive1.bin"};
        text_oarchive oa{file};
        std::map<int,int> m;
        m[3] = 9;
        oa << m;
    }
}

void load()
{
    std::ifstream file{"archive1.bin"};
    text_iarchive ia{file};
    std::map<int,int> pobjr;
    ia >> pobjr;
    std::cout<<pobjr[3]<<std::endl;
}

int main()
{
    save();
    load();
}

This seems to work for me.
At least it prints 9 as expected.

> when i run this program even though the main function runs multiple times the file is read only once at starting and the data can be read instantly inside the main or any function?
I dunno, maybe by having a main which looks like this.
1
2
3
4
5
6
7
8
9
10
11
12
int main ( ) {
  load();
  while ( true ) {
    // do some guff for as long as you want

    // when you've finally had enough, break out of the loop...
    if ( someCondition ) break;
  }

  // and save your data for next time
  save();
}

It sounds like you're streaming the data. Would a pipe be more appropriate?

Memory mapped data is tricky. When you map the file in the writing program, it might appear at one address. Then when you map it in the reading program, it might appear at a different address. That means that all the data within must be position-independent. You can't have any pointers, only offsets to pointers.
Topic archived. No new replies allowed.