如何使用 boost 从 mmap'd 文件中读取异构二进制数据?

How can I read heterogeneous binary data from a mmap'd file using boost?

本文关键字:读取 文件 异构 二进制 数据 boost 何使用 mmap      更新时间:2023-10-16

我构建了一个小的测试应用程序,用于使用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);