【发布时间】:2019-05-20 06:16:38
【问题描述】:
我想读取文件的前 7 个字节。这是我文件的数据:
42 4d b6 fc 0a 00 00
我使用这段代码来做到这一点:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream r("TestFile.abc", ios::binary);
unsigned char info[7];
for(int i = 0 ; i < 7; i++){
r >> info[i];
}
for(int i = 0 ; i < 7; i++){
std::stringstream ss;
ss << std::hex << (int) info[i]; // int decimal_value
std::string res ( ss.str() );
cout << i << setw(10) << info[i] << setw(10) << res << endl;
}
return 0;;
}
这是输出:
0 B 42
1 M 4d
2 ╢ b6
3 ⁿ fc
4 0
5 0
6 0
Process returned 0 (0x0) execution time : 0.032 s
Press any key to continue.
为什么0a字节被0取代了?
【问题讨论】: