如何诊断保存和加载位向量(std::vector)的奇怪行为<bool>?

How to diagnose bizarre behavior of saving and loading a bit vector (std::vector<bool>)?

本文关键字:lt 何诊断 gt bool 加载 保存 向量 vector std 诊断      更新时间:2023-10-16

我正在编写一个一次性实用程序来编辑游戏的单色位图格式。8x8 单色精灵有0x10000"插槽"。我将每个 8x8 精灵存储在八个字节中,每个字节代表一条水平线的开像素或关像素。

当我在插槽 0 到 24 中绘制字符 A 到 Y 时,一切都很好。它们都在以完全相同的位模式进行保存和加载的往返旅行中幸存下来。但随后插槽 25 中的 Z 绘图在往返过程中丢失了一条水平线。更糟糕的是,无论 Z 在哪里,都会发生这种情况,并使其下方的所有线条向上移动!我注意到 25 岁之后插槽中其他模式的其他类似行为。

我的代码看起来一次只检查一个像素,所以我不知道如何诊断这个问题。

据我所知,问题是删除0x0C字节。这似乎不太可能是ASCII表单馈送(^L'f')字符的问题。

我没有找到任何关于缺少表单提要字符的 Google 结果,所以我猜这是我代码中的一个错误。

这是保护程序和加载程序。(这不是我编写已发布或生产代码的方式!

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <SDL.h>
#include <stdint.h>
static std::vector<bool> bitmap(0x400000, 0);
void save(const char *path)
{
std::ofstream f(path, std::ios::binary);
for (int i = 0; i < 0x10000; ++i)
for (int j = 0; j < 8; ++j) {
uint8_t byte = 0;
for (int k = 0; k < 8; ++k)
byte |= bitmap[8 * (8 * i + j) + k] << (7 - k);
f << byte;
}
f.close();
std::cout << "Wrote charmap to " << path << std::endl;
}
void load(const char *path)
{
std::ifstream f(path, std::ios::binary);
for (int i = 0; i < 0x10000; ++i)
for (int j = 0; j < 8; ++j) {
uint8_t byte;
f >> byte;
for (int k = 0; k < 8; ++k)
bitmap[8 * (8 * i + j) + k] = !!(byte & (1 << (7 - k)));
}
f.close();
std::cout << "Read charmap from " << path << std::endl;
}
int main(int argc, char *argv[]) { /* ... snip ... */ }

我希望保留0x0C字节,但它们被删除了。感谢您的任何指示!

处理二进制文件时不要使用格式化流运算符(f << ...;f >> ...;),即使它们以二进制模式打开也是如此。 您不想要格式化的输入/输出,您希望按原样写入/读取字节。 请改用ofstream::write()ifstream::read()方法,例如:

//f << byte;
f.write(reinterpret_cast<char*>(&byte), sizeof(byte));
//f >> byte;
f.read(reinterpret_cast<char*>(&byte), sizeof(byte));