Simplest way to read a CSV file mapped to memory?

后端 未结 2 1032
没有蜡笔的小新
没有蜡笔的小新 2020-12-11 09:49

When I read from files in C++(11) I map them in to memory using:

boost::interprocess::file_mapping* fm = new file_mapping(path, boost::interprocess::read_onl         


        
2条回答
  •  鱼传尺愫
    2020-12-11 10:34

    Simply create an istringstream from your memory mapped bytes and parse that using :

    const std::string stringBuffer(bytes, region->get_size());
    std::istringstream is(stringBuffer);
    typedef boost::tokenizer< boost::escaped_list_separator > Tokenizer;
    std::string line;
    std::vector parsed;
    while(getline(is, line))
    {
        Tokenizer tokenizer(line);
        parsed.assign(tokenizer.begin(),tokenizer.end());
        for (auto &column: parsed)
        {
            // 
        }
    }
    

    Note that on many systems memory mapping isn't providing any speed benefit compared to sequential read. In both cases you will end up reading the data from the disk page by page, probably with the same amount of read ahead, and both the IO latency and bandwidth will be the same in both cases. Whether you have lots of memory or not won't make any difference. Also, depending on the system, memory_mapping, even read-only, might lead to surprising behaviours (e.g. reserving swap space) that don't that sometimes keep people busy troubleshooting.

提交回复
热议问题