0
votes

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):

bytefile

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
Declare bytes as an unsigned char array or cast bytes[i] to unsigned char.Captain Obvlious
tnx this solves the long FFFFFF problem. But the values are not what is in the hex editorWalter
Use outfile.write() instead of the operator<< to store binary data.πάντα ῥεῖ

1 Answers

4
votes
  • 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 and stream.write functions (it's better read and write by block too).
  • If you need to storage fixed binary data use std::array or std::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]);
    }
}