C++中的流类型,如何从IstringStream中读取
Stream types in C++, how to read from IstringStream?
我有一个txt文件,它有数百万行,每行有3个浮动,我使用以下代码读取它:
ifstream file(path)
float x,y,z;
while(!file.eof())
file >> x >> y >> z;
我工作得很好。
现在我想尝试使用Boost映射文件做同样的事情,所以我做了以下
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没有得到实际的浮点数,只是垃圾,一次迭代后While就结束了。
我可能做错了什么
如何使用和解析内存映射文件中的数据?我搜索了整个互联网,尤其是堆栈溢出,找不到任何例子。
我使用的是windows 7 64位。
Boost有一个专门为这个目的制作的库: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';
}
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 如何使用iStringstream读取和解析文本文件?
- 在C++中使用istringstream读取输入时出错
- 有没有办法我可以从 istringstream 中读取两个字符
- istringstream将字符串放回输入并再次读取
- 无法使用 istringstream 从.txt文件中读取
- 为什么我在尝试从istringstream读取数据时出错
- 使用GetLine和Strtok以某种方式从Istringstream读取
- 读取超出 istringstream 字符串末尾的内容。
- 如何从 std::istringstream 读取单词而不通过 >> 分配给 tmp 变量?
- 初学者:从文件中读取并使用istringstream
- 从 istringstream 读取时出现意外行为
- 从 istringstream 读取字符时出现意外行为
- 如何使用 istringstream 从 c++ 中的字符串中读取双精度
- C++使用istringstream将int读取为无符号字符
- (C++)std::istringstream从字符串到双精度最多可读取6位数字
- C++中的流类型,如何从IstringStream中读取
- 从 istringstream 中读取行,末尾没有'r'字符
- 如何通过字段读取和验证istringstream
- 使用getline和istringstream从CSV文件中读取数据