在C ++流类型,如何从IstringStream看? [英] Stream types in C++, how to read from IstringStream?

查看:189
本文介绍了在C ++流类型,如何从IstringStream看?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了百万行的txt文件,每行有3辆彩车,我用下面的code阅读:

i have a txt file that has millions of lines, each line has 3 floats, i read it using the following code :

ifstream file(path)
float x,y,z;
while(!file.eof())
  file >> x >> y >> z;

和我的作品完美。

现在我想尝试使用Boost映射文件做同样的事情,所以我做了以下

Now i want to try doing the same thing using Boost mapped files, so i do the following

string filename = "C:\\myfile.txt";
file_mapping mapping(filename.c_str(), read_only);
mapped_region mapped_rgn(mapping, read_only);
char* const mmaped_data = static_cast<char*>(mapped_rgn.get_address());
streamsize const mmap_size = mapped_rgn.get_size();

istringstream s;
s.rdbuf()->pubsetbuf(mmaped_data, mmap_size);
while(!s.eof())
  mystream >> x >> y >> z;

它编译没有任何问题,但不幸的是在X,Y,Z并没有得到实际的浮点数,但只是垃圾,还有一个迭代后,虽然结束了。

It compiles without any problem, but unfortunatly the X,Y,Z doesn't get the actual float numbers but just rubbish, and after one iteration the While is ended.

我可能做一些可怕的错误。

I probably doing something terribly wrong

我如何使用和解析内存映射文件中的数据?
我找遍了所有在互联网上,特别是堆栈溢出,找不到任何的例子。

How can i use and parse the data inside the memory mapped file ? i searched all over the internet and especially stack overflow and couldn't find any example.

我使用Windows 7 64位。

I'm using windows 7 64 bit.

推荐答案

升压刚刚为此做了一个库:的了Boost.Iostreams

Boost has a library made just for this purpose: boost.iostreams

#include <iostream>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/mapped_file.hpp>
namespace io = boost::iostreams;

int main()
{
    io::stream<io::mapped_file_source> str("test.txt");
    // you can read from str like from any stream, str >> x >> y >> z
    for(float x,y,z; str >> x >> y >> z; )
        std::cout << "Reading from file: " << x << " " << y << " " << z << '\n';
}

这篇关于在C ++流类型,如何从IstringStream看?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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