istream与内存映射文件? [英] istream vs memory mapping a file?

查看:70
本文介绍了istream与内存映射文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件映射到内存,然后逐行解析-istream应该使用什么吗?

I am trying to map a file to memory and then parse line by line- is istream what I should be using?

istream是否与Windows上将文件映射到内存相同?我很难找到一个将文件映射到内存的完整示例.

Is istream the same as mapping a file to memory on Windows? I have had difficulties trying to find a complete example of mapping a file into memory.

我已经看到人们链接了MSDN上的内存映射文章,但是如果有人可以举一个小例子(〜15行?),我将非常感谢.

I have seen people link memory mapping articles from MSDN, but if anybody could recommend a small (~15 line?) example I would be most thankful.

我肯定在搜索错误的内容,但是在Google上搜索"C ++内存映射示例"时,找不到包含迭代过程的示例.

I must be searching for the wrong thing, but when searching "C++ memory mapping example" on Google, I could not find an example that included iterating through.

这些是最接近的结果(只是人们意识到我已经看过):

These were the closest results (just so people realize I have looked):

  • http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2044.html#ClassSharedMemoryObjectExample
  • http://msdn.microsoft.com/en-us/library/dd997372.aspx (no C++ code)
  • https://beej.us/guide/bgipc/html/multi/mmap.html (was for Unix I believe, not Windows)

推荐答案

std :: istream 是抽象类型–您不能直接使用它.您应该使用自定义数组支持的 streambuf :

std::istream is an abstract type – you cannot use it directly. You should be deriving from it with a custom array-backed streambuf:

#include <cstddef>
#include <string>
#include <streambuf>
#include <istream>

template<typename CharT, typename TraitsT = std::char_traits<CharT>>
struct basic_membuf : std::basic_streambuf<CharT, TraitsT> {
    basic_membuf(CharT const* const buf, std::size_t const size) {
        CharT* const p = const_cast<CharT*>(buf);
        this->setg(p, p, p + size);
    }

    //...
};

template<typename CharT, typename TraitsT = std::char_traits<CharT>>
struct basic_imemstream
: virtual basic_membuf<CharT, TraitsT>, std::basic_istream<CharT, TraitsT> {
    basic_imemstream(CharT const* const buf, std::size_t const size)
    : basic_membuf(buf, size),
      std::basic_istream(static_cast<std::basic_streambuf<CharT, TraitsT>*>(this))
    { }

    //...
};

using imemstream = basic_imemstream<char>;

char const* const mmaped_data = /*...*/;
std::size_t const mmap_size = /*...*/;
imemstream s(mmaped_data, mmap_size);
// s now uses the memory mapped data as its underlying buffer.

对于内存映射本身,我建议为此使用 Boost.Interprocess 目的:

As for the memory-mapping itself, I recommend using Boost.Interprocess for this purpose:

#include <cstddef>
#include <string>
#include <boost/interprocess/file_mapping.hpp>
#include <boost/interprocess/mapped_region.hpp>

namespace bip = boost::interprocess;

//...

std::string filename = /*...*/;
bip::file_mapping mapping(filename.c_str(), bip::read_only);
bip::mapped_region mapped_rgn(mapping, bip::read_only);
char const* const mmaped_data = static_cast<char*>(mapped_rgn.get_address());
std::size_t const mmap_size = mapped_rgn.get_size();


此答案通过DietmarKühl.

这篇关于istream与内存映射文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆