为什么从二进制文件读取此流停止在0x1A字符处?

时间:2015-09-26 11:00:40

标签: c++ io filestream

我发现0x1A是替代,但我不确定这意味着什么以及为什么流读取停止。我当然也想知道如何解决这个问题。

首先,我使用以下代码生成二进制文件:

unsigned char bytes[] = { 
    0xA1, 0x58, 0x02, 0x01, 0x03, 0x02, 0x01, 0x15, 0x30, 0x50, 0x55, 0x04, 0x02, 0x00, 0x00, 0x00, 
    0xA7, 0x48, 0xA0, 0x46, 0xA4, 0x44, 0xA7, 0x42, 0x04, 0x40, 0x8A, 0x00, 0x35, 0x2C, 0x77, 0x58, 
    0x83, 0xE9, 0x78, 0x9F, 0x4F, 0x34, 0x06, 0xE1, 0xB2, 0x57, 0x3A, 0x0C, 0xEF, 0x18, 0x54, 0x54, 
    0x1A, 0x4D, 0xBF, 0x2D, 0x02, 0x36, 0x18, 0xCB, 0x4A, 0xC3, 0x58, 0x20, 0xFC, 0x92, 0xDE, 0xBC, 
    0x85, 0x94, 0x90, 0x27, 0x36, 0x05, 0xEA, 0xE5, 0xEA, 0xE6, 0x0B, 0x73, 0xEF, 0xD1, 0x9E, 0x85, 
    0x38, 0x57, 0x1E, 0xF6, 0x62, 0x9B, 0x8F, 0x74, 0xA3, 0x00 };

const size_t sz = sizeof(bytes) / sizeof(bytes[0]);

std::ofstream fstrm("binfile.bin", std::ofstream::binary);
std::for_each(bytes, bytes + sz, [&](const char ch) {
    fstrm.put(ch);
});
fstrm.close();

然后我使用C ++ std lib IO函数来读取流,如下所示:

#include <iostream>
#include <iterator>     // std::istream_iterator
#include <algorithm>


int main() {

    unsigned char bytes[] = {
        0xA1, 0x58, 0x02, 0x01, 0x03, 0x02, 0x01, 0x15, 0x30, 0x50, 0x55, 0x04, 0x02, 0x00, 0x00, 0x00,
        0xA7, 0x48, 0xA0, 0x46, 0xA4, 0x44, 0xA7, 0x42, 0x04, 0x40, 0x8A, 0x00, 0x35, 0x2C, 0x77, 0x58,
        0x83, 0xE9, 0x78, 0x9F, 0x4F, 0x34, 0x06, 0xE1, 0xB2, 0x57, 0x3A, 0x0C, 0xEF, 0x18, 0x54, 0x54,
        0x1A, 0x4D, 0xBF, 0x2D, 0x02, 0x36, 0x18, 0xCB, 0x4A, 0xC3, 0x58, 0x20, 0xFC, 0x92, 0xDE, 0xBC,
        0x85, 0x94, 0x90, 0x27, 0x36, 0x05, 0xEA, 0xE5, 0xEA, 0xE6, 0x0B, 0x73, 0xEF, 0xD1, 0x9E, 0x85,
        0x38, 0x57, 0x1E, 0xF6, 0x62, 0x9B, 0x8F, 0x74, 0xA3, 0x00 };

    const size_t sz = sizeof(bytes) / sizeof(bytes[0]);

    std::cout << "cout with array\n";
    std::for_each(bytes, bytes+sz, [](unsigned char ch) {
        std::cout << std::hex << (int)ch << ' ';
    });
    std::cout << "\n\n";

    std::istream_iterator<unsigned char> eos;              // end-of-stream iterator
    std::istream_iterator<unsigned char> iit(std::cin);   // stdin iterator

    std::cout << "cout with stdin - redirecting file of same bytes as above\n";
    std::for_each(iit, eos, [](unsigned char ch) { 
        std::cout << std::hex << (int)ch << ' ';  
    });

    std::cout << '\n';
}

如果我这样打电话给我的节目:

myprog <binfile.bin

我得到的输出是:

cout with array
a1 58 2 1 3 2 1 15 30 50 55 4 2 0 0 0 a7 48 a0 46 a4 44 a7 42 4 40 8a 0 35 2c 77
 58 83 e9 78 9f 4f 34 6 e1 b2 57 3a c ef 18 54 54 1a 4d bf 2d 2 36 18 cb 4a c3 5
8 20 fc 92 de bc 85 94 90 27 36 5 ea e5 ea e6 b 73 ef d1 9e 85 38 57 1e f6 62 9b
 8f 74 a3 0

cout with stdin - redirecting file of same bytes as above
a1 58 2 1 3 2 1 15 30 50 55 4 2 0 0 0 a7 48 a0 46 a4 44 a7 42 4 40 8a 0 35 2c 77
 58 83 e9 78 9f 4f 34 6 e1 b2 57 3a ef 18 54 54

为什么输入读数停在0xA1字符处?如何获取代码以读取完整的二进制文件?

1 个答案:

答案 0 :(得分:1)

因为0x1A是“Ctrl + Z”,Windows上的“输入结束”字符,并且您正在以文本模式从 stdin 中读取。

You can set the stream into binary mode,但不是以平台无关的方式。通常,如果要进行便携式写入,请避免从 stdin 中获取二进制输入 - 而是将文件名作为命令行参数传递。