使用Boost MMAP阅读文本文件 [英] Read Text file using Boost mmap

查看:576
本文介绍了使用Boost MMAP阅读文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读以下文件:

FILE.TXT

Y:\\测试\\ DOCUMENTS \\文档\\流量保证
  2 \\多\\ ACTEPROC_OK \\ 018-1-R.xml Y:\\测试\\ DOCUMENTS \\文档\\通量
  保证2 \\多\\ ACTEPROC_OK \\ A018-2,R.xml
  Y:\\测试\\ DOCUMENTS \\文档\\流量保证
  2 \\多\\ ACTEPROC_OK \\ 021-1-R.xml Y:\\测试\\ DOCUMENTS \\文档\\通量
  保证2 \\多\\ ACTEPROC_OK \\ A021-2,R.xml
  Y:\\测试\\ DOCUMENTS \\文档\\流量保证
  2 \\多\\ ACTEPROC_OK \\ 022-1-R.xml Y:\\测试\\ DOCUMENTS \\文档\\通量
  保证2 \\多\\ ACTEPROC_OK \\ 022-2-R.xml
  Y:\\测试\\ DOCUMENTS \\文档\\流量保证
  2 \\多\\ ACTEPROC_OK \\ 025-1-R.xml

Y:\Test\DOCUMENTS\DOCUMENTS\Flux Assurance 2\multi\ACTEPROC_OK\018-1-R.xml Y:\Test\DOCUMENTS\DOCUMENTS\Flux Assurance 2\multi\ACTEPROC_OK\A018-2-R.xml Y:\Test\DOCUMENTS\DOCUMENTS\Flux Assurance 2\multi\ACTEPROC_OK\021-1-R.xml Y:\Test\DOCUMENTS\DOCUMENTS\Flux Assurance 2\multi\ACTEPROC_OK\A021-2-R.xml Y:\Test\DOCUMENTS\DOCUMENTS\Flux Assurance 2\multi\ACTEPROC_OK\022-1-R.xml Y:\Test\DOCUMENTS\DOCUMENTS\Flux Assurance 2\multi\ACTEPROC_OK\022-2-R.xml Y:\Test\DOCUMENTS\DOCUMENTS\Flux Assurance 2\multi\ACTEPROC_OK\025-1-R.xml

来源$ C ​​$ C:

#include <iostream>
#include <vector>
#include <boost/iostreams/device/mapped_file.hpp>
#include <boost/iostreams/stream.hpp>

std::vector<string> readFile(string);

int main()
{
    std::vector<string> res = readFile("file.txt");
    return 0;
}

std::vector<string> readFile(string f)
{
    boost::iostreams::stream<boost::iostreams::mapped_file_source> str(f);
    std::vector<string> app;
    for(string x; str >> x;)
    {
        app.push_back(x);
    }
    return app;
}//end


问题:

由于有作为分隔符之间的流量 2保证,它认为空格的空间。

Since There is a space Between Flux and Assurance 2, it considers space as a separator.

如何删除分隔符,或明确标识​​分离器是什么?

How can remove the separator, or explicitly identify what the separator is?

推荐答案

这有什么用升压或memorymapping。

This has nothing to with boost or memorymapping.

首先

str.unsetf(std::ios::skipws); 

将prevent空格被用作分隔符/跳过。

will prevent whitespace from being used as delimiter/skipped.

其次,我 认为(你不提任何的),你想通过行:

Secondly, I think (you don't mention any of that) you wanted to read by line:

for(std::string x; std::getline(str, x, '\n');)
{
    app.push_back(x);
}

正如你可以看到你已经可以指定分隔符。

As you can see you can already specify the delimiter.

最后,可以考虑使用一个解析器生成。例如参见这里:

Lastly, consider using a parser generator. See e.g. here:

  • How to parse space-separated floats in C++ quickly?

其中包含使用升压精神从内存映射文件解析的例子。

Which contains an example that uses Boost Spirit to parse from a memory-mapped file.

这篇关于使用Boost MMAP阅读文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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