[英]How can I read heterogeneous binary data from a mmap'd file using boost?
我建立了一个小测试应用程序,以使用boost::iostreams::mapped_file_source
从文件读取二进制数据。
不幸的是,我的垃圾越来越多了-所以很明显不能正确读取数据。
我的假设是我可以将data()
指针强制转换为期望的任何类型,但是我认为这是不正确的。
const char* data = file.data();
uint64_t f1 = (uint64_t)*data; // incorrect
证明:
我使用std::ifstream
构建了另一个测试应用程序以验证数据,并且该应用程序按预期工作。
题:
如何从boost::iostreams::mapped_file_source
读取异构二进制数据?
ifstream
和损坏的boost
测试应用程序都在下面。
使用std::ifstream
读取二进制数据的工作示例:
#include <iostream>
#include <fstream>
template<typename T>
void read(std::ifstream& ifs, T& data)
{
ifs.read(reinterpret_cast<char*>(&data), sizeof(T));
}
int main()
{
std::ifstream ifs("/tmp/data", std::ios::in | std::ios::binary);
uint64_t f1;
int32_t f2
double f3;
while(1)
{
read(ifs, f1);
read(ifs, f2);
read(ifs, f3);
if (ifs.eof())
break;
std::cout << f1 << ' ' << f2 << ' ' << f3 << '\n';
}
return 0;
}
输出(正确):
1463071170459690752 400 90.08 1463071170504337152 400 90.08 1463071170561888256 300 90.08 1463071170561923328 400 90.08 1463071170561973760 500 90.08
使用boost::iostreams::mapped_file_source
读取二进制数据的错误示例:
#include <boost/iostreams/device/mapped_file.hpp>
#include <iostream>
int main()
{
boost::iostreams::mapped_file_source file;
file.open("/tmp/data");
int size = file.size();
const char* data = file.data();
uint64_t f1;
int32_t f2
double f3;
while (size > 0)
{
f1 = (uint64_t)*data; data += sizeof(uint64_t); size -= sizeof(uint64_t);
f2 = (int32_t)*data; data += sizeof(int32_t); size -= sizeof(int32_t);
f3 = (double)*data; data += sizeof(double); size -= sizeof(double);
std::cout << f1 << ' ' << f2 << ' ' << f3 << '\n';
}
return 0;
}
输出(不正确):
0 -112 -123 0 -112 -123 0 44 -123 0 -112 -123 0 -12 -123
您所做的转换错误。 我会给你一个例子,其余的你可以解决。
f1 = (uint64_t)*data;
这将取消对char*
引用-给您一个1字节的值-然后将该1字节的值转换为64位int! 绝对不是你想做的。 相反,您需要这样做:
f1 = *(uint64_t*)data;
而且由于我确实喜欢显式的,所以更好,
f1 = *reinterpret_cast<uint64_t*>(data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.