小编Bur*_*ium的帖子

istream_iterator迭代二进制文件中的字节

给定包含以下十六进制代码的文件:0B 00 00 00 00 00 20 41

我正在尝试填充std :: vector <std :: uint8_t>,然后手动检查每个字节.

这是我使用迭代器构造函数从两个std :: istream_iterators创建向量的代码

using Bytes   = std::vector<std::uint8_t>;
using ByteItr = std::istream_iterator<std::uint8_t>;

Bytes getBytes()
{
    std::ifstream in;
    in.open("filepath");
    in.seekg(0, std::ios::beg);
    Bytes bytes;
    ByteItr start(in);
    ByteItr end;
    return Bytes(start, end);
}
Run Code Online (Sandbox Code Playgroud)

这是我试图通过的单元测试:

auto bytes = getBytes();

REQUIRE( bytes.size() == 8 );

CHECK( bytes[0] == 0x0B );
CHECK( bytes[1] == 0x00 );
CHECK( bytes[2] == 0x00 );
CHECK( bytes[3] == 0x00 );
CHECK( bytes[4] == 0x00 ); …
Run Code Online (Sandbox Code Playgroud)

c++ hex vector istream-iterator c++11

1
推荐指数
2
解决办法
2043
查看次数

标签 统计

c++ ×1

c++11 ×1

hex ×1

istream-iterator ×1

vector ×1