c++ boost write memory mapped file

2019-04-01 19:06发布

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.

3条回答
放我归山
2楼-- · 2019-04-01 19:27

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"));
}
查看更多
仙女界的扛把子
3楼-- · 2019-04-01 19:32

There is a boost library specifically for exactly this. It's called "boost interprocess".

The documentation (with examples) is here:

http://www.boost.org/doc/libs/1_59_0/doc/html/interprocess.html

查看更多
放荡不羁爱自由
4楼-- · 2019-04-01 19:32

I know of no portable way to do memory-mapped files (though maybe boost has something, boost usually does...). Are you using Linux? Then there are good mechanisms for writing high-performance stuff with memory-mapped files, e.g. mmap(2). Those mechanisms are also partly portable to other major Unix OS:es. (Yes, Linux is Unix) For some applications, using threads (that of course share virtual memory space) is an alternative. Then you do not have portability problems.

查看更多
登录 后发表回答