【问题标题】:Reading and outputting byte at position in binary file (C++)在二进制文件中的位置读取和输出字节(C++)
【发布时间】:2015-04-11 04:11:29
【问题描述】:

我想从二进制 (.bin) 文件中读取一组 256 个字节并将其存储到缓冲区中。然后我想将这些字节中的每一个移动到一个 char 向量的向量中。另外,我想读取向量中的 char 值并获取该字节二进制值的相应整数值。这就是我的处理方式:

typedef unsigned char BYTE;
vector< vector<BYTE> > pMem (256, vector<BYTE> (256, '0'));
ifstream binStream;
binStream.open("BACKING_STORE.bin", ios::binary | ios::in );
// allocate memory:
char * buffer = new char [256];

binStream.seekg(0, binStream.beg);
binStream.read(buffer, 256);

for( i = 0; i < 256; i++ )
    pMem[0][i] = buffer[i];

for( i = 0; i < 256; i++ )
{
    cout << "Physical memory char contents at 0[" << i << "]: " << pMem[0][i] << endl;
    int x = pMem[0][i];
    cout << "Physical memory integer contents at 0[" << i << "]: " << x << endl;
}

但是当我打印出这些值时,我会得到如下结果:

Physical memory char contents at 0[237]: 
Physical memory integer contents at 0[237]: 0
Physical memory char contents at 0[238]: 
Physical memory integer contents at 0[238]: 0
Physical memory char contents at 0[239]: ;
Physical memory integer contents at 0[239]: 59
Physical memory char contents at 0[240]: 
Physical memory integer contents at 0[240]: 0
Physical memory char contents at 0[241]: 
Physical memory integer contents at 0[241]: 0
Physical memory char contents at 0[242]: 
Physical memory integer contents at 0[242]: 0
Physical memory char contents at 0[243]: <
Physical memory integer contents at 0[243]: 60
Physical memory char contents at 0[244]: 
Physical memory integer contents at 0[244]: 0
Physical memory char contents at 0[245]: 
Physical memory integer contents at 0[245]: 0
Physical memory char contents at 0[246]: 
Physical memory integer contents at 0[246]: 0
Physical memory char contents at 0[247]: =
Physical memory integer contents at 0[247]: 61
Physical memory char contents at 0[248]: 
Physical memory integer contents at 0[248]: 0
Physical memory char contents at 0[249]: 
Physical memory integer contents at 0[249]: 0
Physical memory char contents at 0[250]: 
Physical memory integer contents at 0[250]: 0
Physical memory char contents at 0[251]: >
Physical memory integer contents at 0[251]: 62
Physical memory char contents at 0[252]: 
Physical memory integer contents at 0[252]: 0
Physical memory char contents at 0[253]: 
Physical memory integer contents at 0[253]: 0
Physical memory char contents at 0[254]: 
Physical memory integer contents at 0[254]: 0
Physical memory char contents at 0[255]: ?
Physical memory integer contents at 0[255]: 63

这显然是错误的输出。有人可以解释一下我做错了什么以及如何做到这一点吗?

【问题讨论】:

    标签: c++ file-io binaryfiles seekg


    【解决方案1】:

    当你有:

    char c = 'a';
    std::cout << c << std::endl;
    

    输出将是

    a
    

    c 的值对应于非打印字符之一时,输出因系统而异。在您的情况下,您得到的是空白输出。

    如果你想打印'a' 的整数表示,你必须强制转换它。

    std::cout << (int)c << std::endl;
    

    int x = c;
    std::cout << x << std::endl;
    

    我希望这就是您需要的全部解释。

    【讨论】:

    • 我尝试了第二种方法:int x = pMem[0][i]; cout
    • @Caulibrot,我在您的代码中看不到任何其他错误。我无法解释为什么它不起作用。另外,我不知道你为什么认为它不起作用。请在您的帖子中添加预期输出和您得到的输出。
    • 啊,我知道我哪里出错了。这些值是要签名的,我使用的是无符号字符。嗬!感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    • 2021-09-07
    • 2013-12-06
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多