Bur*_*ium 1 c++ hex vector istream-iterator c++11
给定包含以下十六进制代码的文件: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 );
CHECK( bytes[5] == 0x00 );
CHECK( bytes[6] == 0x20 );
CHECK( bytes[7] == 0x41 );
Run Code Online (Sandbox Code Playgroud)
为什么在这种情况下,它会跳过两个元素并隐式将我的std :: uint8_t向量转换为无符号字符?
Ben*_*ley 11
istream_iterator不应该用于读取二进制文件.它使用operator>>,也不适合读取二进制文件(除非这些文件是大多数二进制文件不适合的特定格式).你可以istreambuf_iterator改用.您还希望确保以二进制模式打开文件.
in.open("filepath", std::ios::in | std::ios::binary);
Run Code Online (Sandbox Code Playgroud)
不要使用std::istream_iterator<T>:那是用于文本格式的输入.例如,它很可能会跳过空格(你可以禁用跳过空格std::noskipws,但这仍然是错误的事情 - std::istreambuf_iterator<char>改为使用;类型char是流的字符类型).
此外,在处理二进制数据时,请确保以二进制模式打开流以避免行结束转换(如果您在执行行结束转换的平台上尝试此操作).也就是说,您将添加std::ios_base::binary到打开模式.
| 归档时间: |
|
| 查看次数: |
2043 次 |
| 最近记录: |