c++ boost write memory mapped file

跟風遠走 提交于 2019-12-09 07:14:13

问题


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.


回答1:


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"));
}



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/33051067/c-boost-write-memory-mapped-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!