bla*_*pup 2 c++ boost memory-mapped-files
我正在寻找使用C++和boost库快速编写文件.我想使用内存映射文件.但几乎所有的例子都是关于阅读.
工作很简单.有一个字符串数组.数组元素大约有2百万.
ofstream outFile("text.txt");
for (int i = 0; i < 2000000; ++i) {
outFile << strArray[i] << "\n";
}
outFile.close();
Run Code Online (Sandbox Code Playgroud)
我怎么能用内存映射文件呢?我在哪里可以找到使用内存映射文件的文件?
谢谢你的关心.
你可以使用Boost Iostreams mapped_file{_sink,_source}.
尽管Boost Interprocess确实使用了映射文件,但您最好使用IOstream进行这种原始访问.
请参阅http://www.boost.org/doc/libs/1_50_0/libs/iostreams/doc/classes/mapped_file.html
#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"));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5959 次 |
| 最近记录: |