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.
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
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
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.