问题
Im trying to read a byte file with fstream in c++ (goal: binary data format deserialization). The dat file looks something like below in HxD Hex editor (bytes.dat):
but something goes wrong when reading the binary file into a char array.. here is a mwe:
#include <iostream>
#include <fstream>
using namespace std;
int main (){
ofstream outfile;
outfile.open("bytes.dat", std::ios::binary);
outfile << hex << (char) 0x77 << (char) 0x77 << (char) 0x77 << (char) 0x07 \
<< (char) 0x9C << (char) 0x04 << (char) 0x00 << (char) 0x00 << (char) 0x41 \
<< (char) 0x49 << (char) 0x44 << (char) 0x30 << (char) 0x00 << (char) 0x00 \
<< (char) 0x04 << (char) 0x9C;
ifstream infile;
infile.open("bytes.dat", ios::in | ios::binary);
char bytes[16];
for (int i = 0; i < 16; ++i)
{
infile.read(&bytes[i], 1);
printf("%02X ", bytes[i]);
}
}
but this shows in the cout (mingw compiled):
> g++ bytes.cpp -o bytes.exe
> bytes.exe
6A 58 2E 76 FFFFFF9E 6A 2E 76 FFFFFFB0 1E 40 00 6C FFFFFFFF 28 00
im doing something wrong. How is it possible that there 4 bytes in some of the array entries?
回答1:
- When working with binary data (binary file format and like), it's better to work with unsigned integer type to avoid sign extension conversions.
- As recommended when reading and writing binary data, better use
stream.read
andstream.write
functions (it's better read and write by block too). - If you need to storage fixed binary data use
std::array
orstd::vector
, if you need to load data from file (std::vector
is the default)
Fixed code:
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main() {
vector<unsigned char> bytes1{0x77, 0x77, 0x77, 0x07, 0x9C, 0x04, 0x00, 0x00,
0x41, 0x49, 0x44, 0x30, 0x00, 0x00, 0x04, 0x9C};
ofstream outfile("bytes.dat", std::ios::binary);
outfile.write((char*)&bytes1[0], bytes1.size());
outfile.close();
vector<unsigned char> bytes2(bytes1.size(), 0);
ifstream infile("bytes.dat", ios::in | ios::binary);
infile.read((char*)&bytes2[0], bytes2.size());
for (int i = 0; i < bytes2.size(); ++i) {
printf("%02X ", bytes2[i]);
}
}
来源:https://stackoverflow.com/questions/25625115/cpp-byte-file-reading