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;
}
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?
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.
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?
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.