[英]Reading text file with floating point numbers faster and storing in vector of floats
[英]memory mapping a text file of floats separated by space to a vector of floating point numbers
我有一个文本文件,其中有数千个由空格分隔的浮点数。我试图使用内存映射将所有这些数据从文本文件复制到Visual Studio 2010中使用c ++的浮点数向量。以下是代码我用来读取文本文件到memory.Code只是读取随机数,这是没有意义的。 任何人都可以帮我修复它并将数据复制到浮点数向量中
#include<boost\iostreams\device\mapped_file.hpp>
#include<iostream>
int main()
{
boost::iostreams::mapped_file_source file;
int numberofElements = 1000000;
int numberofBytes = numberofElements*sizeof(float);
file.open("ReplayTextFile.txt",numberofBytes);
if(file.is_open())
{
float* data = (float*)file.data();
for(int i = 0;i <numberofElements;i++)
std::cout<<data[i]<<", ";
file.close();
} else
{
std::cout<<std::cout<<" couldnt map the file"<<std::endl;
}
system("pause");
return 0;
}
这基本上是查看输入文本的基础表示,获取其中的sizeof(float)
字节并尝试将其视为实际的float
。
在典型的情况下, float
将是4个字节,因此给定输入如1.23456
,它将需要1.23
,查看基础表示(通常为0x31,0x23,0x32,0x33)并将其解释为float
。
然后它将需要456
(0x34,0x35,0x36,0x20)并将其解释为第二个float
。
显然,您需要将一个数字的数字转换为一个浮点数,忽略空格,然后将下一个数字的数字转换为下一个float
。
最简单的方法是将文件作为流打开,然后从文件初始化的istream_iterator
初始化vector<float>
:
std::ifstream in("ReplayTextFile.txt");
std::vector<float> floats { std::istream_iterator<float>(in),
std::istream_iterator<float>()};
首先,术语狡辩:你不是将文件中的数据复制到浮点数向量中,而是将数据内存映射到浮点数组中。
其次,当您对内存映射文件时,内存的内容实际上与磁盘上文件的内容相同。 因此,如果一个文件的编号为2.203
并且其中没有其他内容,当你的内存映射它并尝试将元素0作为浮点数读取时,你将读取(假设sizeof(float)==4
)字节(十六进制) 32 2e 32 30
。 这些将被解释为浮点数,这不是你想要的。
相反,您需要在某个时刻处理输入并将字符串表示转换为表示该数字作为浮点的字节。 您可以通过使用ifstream
打开文件然后使用>>
运算符读取浮点数来实现。
但是,如果您希望内存映射文件所带来的运行时效率,则可能不希望每次运行程序时都解析浮点数。 在这种情况下,您需要首先预处理文件,将其从一系列数字转换为字符串,而不是包含表示所需浮点数的原始字节。
在我的代码中,我使用下面的函数将字节写入使用ios_base::binary
打开的ostream
。
void writeFloat(std::ostream &out, float f) {
char *pt = reinterpret_cast<char*>(&f);
out.put(pt[0]);
out.put(pt[1]);
out.put(pt[2]);
out.put(pt[3]);
}
准备好文件后,您应该能够存储映射并从中读取数据,因为代码已经存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.