如何使用 boost 从 mmap'd 文件中读取异构二进制数据?
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;
既然我更喜欢explicits,更好的是
f1 = *reinterpret_cast<uint64_t*>(data);
相关文章:
- 使用新行和不使用新行读取文件
- 读取文件并输入到矢量中
- 读取文件的最后一行并输入到链接列表时出错
- 为什么在读取文件大小时文件IO速度会发生变化
- 读取文件时运行时的未知行为
- 如何逐行读取文件,每行中的内容都用空格分隔并将其写入新文件中
- 读取文件中所有可能的十六进制 16 字节序列并打印每个序列
- C++ 读取文件读取文件不正确
- 读取文件并将其存储在unordered_map中时出现问题
- 读取文件时无法使用 OpenMP 获得加速
- 使用istringstearm和get行缓慢读取文件
- 如何继续读取不同功能中的文件,而不是从头开始再次读取文件?
- 读取文件时引发异常
- 从标准输入读取文件后读取用户输入
- 在读取文件后重置句柄
- 如何在C++编译时读取文件?
- std::ifstream 在读取文件中最后一项时设置 eofbit,但仅在读取数值类型时发生
- 读取文件在第二次调用时返回INVALID_HANDLE
- 通过指针读取文件
- 逐行读取文件,并将数据插入变量和数组中