Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ boost write memory mapped file

I am searching for fast writing a file using C++ and boost library. And I would like to use memory mapped file. But Almost all example is about reading.
Working is very simple. There is a string array. The array elements is about 2 millions.

ofstream outFile("text.txt");
for (int i = 0; i < 2000000; ++i) {
    outFile << strArray[i] << "\n";
}
outFile.close();

How can I do it using memory mapped file? Where can I find writing file using memory mapped file?

Thank you for your concerning.

like image 562
blank_popup Avatar asked Dec 06 '22 20:12

blank_popup


1 Answers

You could use Boost Iostreams mapped_file{_sink,_source} for this.

Although Boost Interprocess does use mapped files, but you'd be better off using IOstreams for this kind of raw access.

See http://www.boost.org/doc/libs/1_50_0/libs/iostreams/doc/classes/mapped_file.html

Live On Coliru

#include <boost/iostreams/device/mapped_file.hpp>
#include <boost/iostreams/stream.hpp>
#include <vector>

namespace bio = boost::iostreams;

int main() {
    using namespace std;
    vector<string> strArray(2000000);

    bio::mapped_file_params params;
    params.path          = "text.txt";
    params.new_file_size = 30ul << 30;
    params.flags         = bio::mapped_file::mapmode::readwrite;

    bio::stream<bio::mapped_file_sink> out(params);

    copy(strArray.begin(), strArray.end(), ostream_iterator<string>(out, "\n"));
}
like image 175
sehe Avatar answered Dec 26 '22 04:12

sehe