从二进制文件读取后数字奇怪

时间:2015-02-26 20:41:10

标签: c++ hex decimal binaryfiles 16-bit

我想将十六进制32位数字写入二进制文件,而不是使用reinterpret_cast读取其中一些数字并读取,例如16位数。我只读16位,因为它决定了数据包的大小。在代码中有一个例子。也许这个问题是大端还是小端?

#include <iostream>     // std::cout
#include <fstream>      // std::ifstream
#include <cstdint>
#include <vector>

void saveTestData(void)
{
  std::vector<std::uint_fast32_t> tab
  {
    0x50e5495c, 0xe7b50200, 0xbe6b2248, 0x08004510,
    0x015c2340, 0x0000ff11, 0x1567c0a8, 0x004cc0a8,
    0x003de290, 0xc35a0148, 0x00000000, 0x01200003,
    0x00620000, 0x01140002, 0x00010000, 0x8000ef40,
    0x22560003, 0xe0042150, 0x00006bbf, 0xd67c800f,
    0x5b5b0003, 0xe0032150, 0x00006bbf, 0xd67c8007,
    0x1b5d0003, 0xe0022150, 0x00006bbf, 0xd67c800a,
    0xab5d0023, 0xe0052150, 0x00006bbf, 0xd67c8011,
    0x8b5c6bbf, 0xd67c8c55, 0xaf896bbf, 0xd67c8c90,
    0x4f896bbf, 0xd67c8cd4, 0xef8a6bbf, 0xd67c8d0d,
    0x1f8a6bbf, 0xd67c8d43, 0x7f886bbf, 0xd67c8d8f,
    0x8f896bbf, 0xd67c8dc4, 0xcf886bbf, 0xd67c8e19,
    0x6f896bbf, 0xd67c8e4e, 0x1f8a6bbf, 0xd67c8e82,
    0xcf8a6bbf, 0xd67c8ed7, 0x4f896bbf, 0xd67c8f0c,
    0xef896bbf, 0xd67c8f4f, 0x8f896bbf, 0xd67c8f96,
    0xef8a6bbf, 0xd67c8fdb, 0xcf896bbf, 0xd67c9008,
    0xbf89000e, 0x80001006, 0xf0724646, 0xb45b0000,
    0x00004646, 0xb45b0000, 0x00000000, 0x00000000,
    0x00000000, 0x00000000, 0x00004646, 0xb45b0000,
    0x00004646, 0xb45b0000, 0x00008000, 0x00000001,
    0x55550000, 0x0001aaaa, 0xaaaa0000, 0x01200003,
    0x00620000, 0x01140002, 0x00010000, 0x8000ef40,
    0x22560003, 0xe0042150, 0x0000
  };

  std::ofstream file;
  file.open("test.omgwtf", std::ofstream::binary);
  if(file.good())
  {
    file.write(reinterpret_cast<char*>(tab.data()), tab.size()*sizeof(std::uint_fast32_t));
    file.close();
  }

}

int main()
{
  saveTestData();
  std::ifstream file("test.omgwtf", std::ifstream::binary);
  if(file.good())
  {
    file.seekg(0, file.end);
    uint32_t length = file.tellg();
    file.seekg(0, file.beg);

    char *buffer = new char[length];
    std::cout << "length = " << length << std::endl;
    file.read(buffer, length);

    std::uint_fast32_t *number32 = reinterpret_cast<std::uint_fast32_t*>(buffer);
    std::cout << "1 number32 = " << *number32 << std::endl;     // ok

    number32 = reinterpret_cast<std::uint_fast32_t*>(buffer+4);
    std::cout << "2 number32 = " << *number32 << std::endl;     // ok

    // read 0xbe6b (16 bits not 32)
    // 0xbe6b (hex) = 48747 (dec)
    std::uint_fast16_t *number16 = reinterpret_cast<std::uint_fast16_t*>(buffer+8);
    std::cout << "3 number16 = " << *number16 << std::endl;     // not ok!? why?

    // read 2248 (16 bits not 32)
    // 2248 (hex) = 8776 (dec)
    number16 = reinterpret_cast<std::uint_fast16_t*>(buffer+10);
    std::cout << "4 number16 = " << *number16 << std::endl;     // not ok!? why?

    file.close();
    delete [] buffer;
  }
  return 0;
}

如何读取16号? 1,2例子还可以。 3个例子应该是48747还不是3194692168? 4例应该是8776而不是1158725227?

clear; g++ test2.cpp -std=c++11 -o test2; ./test2

1 个答案:

答案 0 :(得分:2)

std::binary名称很糟糕,它确实控制了换行符。

iostreams仅用于文本。您可以使用没有换行符的文本(使用std::binary,文件最终使用Unix换行符约定,仅使用\n)或使用换行符翻译文本(不要使用std::binary,文件最终遵循操作系统惯例,例如\n\r\n,甚至\r)。

但即使使用std::binary,也可以识别EOF字符(ASCII 26)并结束输入。或不。标准没有说。该标准没有为未翻译的文件访问提供任何机制。

人们一直在努力设计一个更好的C ++标准I / O机制,将文件访问与文本处理分开,但还没有人让每个人都满意。

对于二进制文件,请使用低级I / O机制。甚至<stdio.h>比iostream更好(翻译更少),但它仍然有一些。特定于操作系统的函数或使用下面的OS函数的跨平台包装器(如boost::asio)是二进制文件访问所需的。

此外,您在整个地方都有严格的别名冲突。不要像这样使用reinterpret_cast,而是使用memcpy或从输入文件中单独读取正确大小的块。

最后,您正在读取错误的大小变量。 uint_fast16_t不是16位,它是16位或更多,无论什么是最快的。几乎可以肯定,CPU上的32位比16位快。如果您想要16位,请使用uint16_t。如果您想尽可能接近(但不能更少),请使用uint_least16_tuint_fast类型系列适用于局部变量,例如循环计数器。由于未知的大小,它们对I / O没用。

一旦你弄清楚这一切,你需要担心原始数据的字节顺序,因为它是作为一个32位(或更多)值的序列写入的,无论高半还是低半写入文件首先是平台依赖。

相关问题