读取整数的二进制文件

微信

首先,我将一些 int 变量写入 .bin 文件。然后我试着读回这些数字,但我没有这样做。

我是这样写的:

std::ofstream OutFile;
OutFile.open("encode.bin", std::ios::out | std::ios::binary);

for(int i = 0; i < all.size(); i++){
        int code = codes[i];
        OutFile.write(reinterpret_cast<const char *>(&code), sizeof(int));
}
OutFile.close();

这就是我写数字时我的 .bin 文件的样子: 65, 66, 66, 257, 258, 260

  Offset: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F   
00000000: 41 00 00 00 42 00 00 00 42 00 00 00 01 01 00 00   
00000010: 02 01 00 00 04 01 00 00         

字节序有问题吗?我看到数字颠倒了。

以及我如何阅读它:

std::vector<int> allCodes;
std::ifstream inputD(file, std::ios::binary);

std::vector<char> buffer((
    std::istreambuf_iterator<char>(inputD)),
    (std::istreambuf_iterator<char>()));

for (auto a : buffer) {
    data.push_back(static_cast<int>(a));
    allCodes.push_back(a);
};

当我显示我的向量时,前三个数字(65, 66, 66)被正确读取,中间几乎没有零。

这是显示的样子:

用户3366592

First of all, you shouldn't use reinterpret_cast here, because of the endianness - you lose portability. You write integers which are 4 bytes long in your case. Then you try to read the numbers to char which has only 1 byte. This explains why you see correct output for the first three numbers (they are in range from 0 to 255) and why there are some zeros between them.

Here, I found some piece of code on my hard drive, it probably could be written better, but it does the job and is a bit safer than your solution.

template<typename T> void ReadInteger(T &Output, const char* Buffer)
{
    static_assert(std::numeric_limits<T>::is_integer, "return type cannot be non-arithmetic or floating point");
    Output = 0;
    for(unsigned int i = 0; i<sizeof(T); i++)
    {
        Output <<= 8;
        Output |= Buffer[i];
    }
}

template<typename T> void WriteInteger(T Value, char* Buffer)
{
    static_assert(std::numeric_limits<T>::is_integer, "first parameter cannot be non-arithmetic or floating point");
    for(unsigned int i = 0; i<sizeof(T); i++)
    {
        Buffer[sizeof(T)-i-1] = static_cast<char>(Value&0xff);
        Value >>= 8;
    }
}

Example usage:

int Value = 42;
char Buffer[sizeof(int)];
WriteInteger(Value, Buffer);
File.write(Buffer, sizeof(int));
// ...
File.read(Buffer, sizeof(int));
int a;
ReadInteger(a, Buffer);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从C中的文件读取二进制整数

来自分类Dev

从二进制文件C ++读取16位整数

来自分类Dev

二进制文件写入后读取负整数

来自分类Dev

大块读取二进制文件

来自分类Dev

从二进制文件读取

来自分类Dev

从二进制文件读取char *

来自分类Dev

从MongoDB读取二进制文件

来自分类Dev

从二进制文件读取

来自分类Dev

读取二进制文件c

来自分类Dev

读取大型二进制文件

来自分类Dev

读取/写入二进制文件

来自分类Dev

分批读取二进制文件

来自分类Dev

从二进制文件中读取

来自分类Dev

在二进制文件中保存/读取整数矩阵并读取它们

来自分类Dev

读取已发布的二进制文件并写入新的二进制文件

来自分类Dev

从二进制文件读取无符号整数64位

来自分类Dev

从特定位置的二进制文件读取整数的性能问题

来自分类Dev

在Python中从二进制文件读取4个字节的整数

来自分类Dev

Python-从二进制文件读取10位整数

来自分类Dev

从文件中读取二进制数并将其保存为整数

来自分类Dev

使用迭代器从文件中读取二进制整数列表

来自分类Dev

使用ifstream从C ++文件中读取二进制无符号短整数

来自分类Dev

在C ++中向文件流写入/读取二进制字节/整数/长

来自分类Dev

从二进制文件读取无符号整数64位

来自分类Dev

节点js,如何写入/读取整数到二进制文件?

来自分类Dev

c ++从二进制文件中读取整数,缺少一些数据

来自分类Dev

将从二进制文件读取的字符串转换为整数

来自分类Dev

读取文件为二进制/十六进制

来自分类Dev

从R中的二进制<8位读取整数