【问题标题】:c++ - Failing to read a binary filec++ - 无法读取二进制文件
【发布时间】:2019-05-16 11:02:31
【问题描述】:

我有一个二进制文件,应该主要包含原始字节数据,该文件在CubeState 类中生成和处理。我进行了一些测试,似乎编写过程还可以。我得到的唯一问题是,有时它会记下 0x00 字符,每当发生这种情况时,我都会收到裁剪或没有数据。这是我如何写/读的例子。

CubeState currentParent = CubeState();
char* buf = new char[10];
std::ofstream ostr = std::ofstream("parents", std::ios::out | std::ios::binary);
ostr.write(currentParent.getContent().c_str(), 10);
ostr.close();

std::ifstream istr("parents", std::ios::in | std::ios::binary);     
istr.read(buf, 10);
istr.close();
currentParent = CubeState(buf);

currentParent.getContent().c_str()在默认形式下会返回如下字符串——0x00 0x01 0x05 0x0B 0x17等等。

编辑

CubeState 是正在工作的黑匣子。如果你需要另一个例子 - 给你:

char* buf = new char[4];
std::ofstream ostr = std::ofstream("parents", std::ios::out | 
std::ios::binary);
ostr.write("" + (char)0 + (char)1 + (char)5 + (char)11, 4);
ostr.close();

std::ifstream istr("parents", std::ios::in | std::ios::binary);     
istr.read(buf, 4);
istr.close();
std::cout << buf << std::endl;

输出将为空,如果您在运行时检查buf - 它也是空的

【问题讨论】:

  • write 将写入 10 个字符,无论从 c_str() 返回的字符串有多长。这真的是你想要的吗?如果字符串实际上没有那么长,则这是未定义的行为。
  • 有趣的是,您如何在 CubeState 中处理 buf?也许您的代码会检查 buf 中的 char 是否不是空字符(这可能是因为您正在编写 0x00},然后它会停止处理,让您觉得数据被裁剪或未读取。跨度>
  • getContent() 的输出始终是 10 个字符的字符串。我在运行时检查了buf,它是空的。
  • 如果大小不是问题,那么除了您可能在CubeState(buf) 中处理错误的char 指针外,我没有看到问题,没有考虑到它是二进制文件缓冲区而不是一个以 null 结尾的 C 字符串。
  • 这不是minimal reproducible example。任何程序员要做的第一件事就是对字符串进行硬编码,而不是调用诸如CubeState 之类的函数来查看总体思路是否有效。您发布的代码我们不知道CubeState 做了什么,它是否有错误等。花时间编写一个小的main 函数,它对已知的硬编码字符串数据做同样的事情。如果它工作正常,那么只有在那时你才会引入函数调用。

标签: c++ file io binary binaryfiles


【解决方案1】:

CubeState 倾向于生成以 /0 开头的输出,即(char)0。这导致了问题 - 返回的字符串被假定为空。 一旦我有足够的代码 sn-p 可以正确地证明我的担忧,我将在稍后提出另一个问题。 如果您仍然对CubeState.getContent() 的内容感兴趣 - 在这里

class CubeState {
private:
state = {
0, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26
};
const int av[] = { 0, 0, 1, 1, 0, 2, 2, 3, 3, 4, 0, 5, 0, 0, 0, 6, 0, 7, 4, 8, 5, 9, 0, 10, 6, 11, 7 },
corners[] = { 0, 2, 6, 8, 18, 20, 24, 26 },
sides[] = { 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25 };

... 

std::string getContent() {
std::bitset<80> container(0);
for (int i = 0; i < 8; i++) {
    int temp = av[state[corners[i]]];
    container[i * 3] = temp % 2;
    temp >>= 1;
    container[i * 3 + 1] = temp % 2;
    temp >>= 1;
    container[i * 3 + 2] = temp;
}

for (int i = 0; i < 12; i++) {
    int temp = av[state[sides[i]]];
    container[i * 4 + 24] = temp % 2;
    temp >>= 1;
    container[i * 4 + 25] = temp % 2;
    temp >>= 1;
    container[i * 4 + 26] = temp % 2;
    temp >>= 1;
    container[i * 4 + 27] = temp;
}

int temp = parent;  
container[72] = temp % 2;
temp >>= 1;
container[73] = temp % 2;
temp >>= 1;
container[74] = temp % 2;
temp >>= 1;
container[75] = temp;
std::string result;

for (int i = 0; i < 10; i++) 
    result += std::bitset<8>(container.to_string().substr(i, i * 8)).to_ulong() + 1;

return result;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-18
    • 2017-10-01
    • 2011-09-03
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    相关资源
    最近更新 更多