Carlos Plaza
Carlos Plaza

Reputation: 13

Fill struct with a binary file

I want to fill the following struct:

struct Data{
    char id; // 1 byte
    char date[7]; // 7 bytes
    short int Ny; // 2 bytes
    short int Nx; 
    short int L;
    unsigned char distance;
    short int N;
    std::vector<short int> quant_levels;
    std::vector<std::vector<unsigned char>> Pixels;
};

Based on the information on how to decode my binary file:

Byte           Example                Description
0              1                      Format id (1)
1-7            31-Mar-1998 11:55      Date/Time
18-19                                 Number of rows (short int LSB first) (=NY)
20-21                                 Number of columns (=NX)
22                                    Number of 3D-levels (=L)
23-24                                 distance(m) between the 3D-levels (short int LSB first)
25                                    Number of quantisation levels (=N)
26-26+n*2-1                           quantisation levels (N * short int LSB first) in 1/10mm/h
X1-Y1                                 Pixels (NX*NY Bytes) from upper-left to down-right on the 1st 3D-Level
X2-Y2                                 Pixels (NX*NY Bytes) from upper-left to down-right on the 2nd 3D-Level
XL-YL                                 Pixels (NX*NY Bytes) from upper-left to down-right on the Lth 3D-Level

I want to save fill the struct while reading my binary file so I have implemented this, which is not finished because I do not know how to get a short int out of 2 bytes.

Data readFile(const char* filename)
{
    Data data{};
    // open the file:
    std::fstream fh;
    fh.open(filename, std::fstream::in | std::fstream::binary);

    // read the data:
    fh.read((char*)&data.id, sizeof(char));
    fh.read((char*)&data.date, sizeof(data.date));
    fh.read((char*)&data.Ny, sizeof(data.Ny)); // WRONG, how can I move to byte 18?
    // TODO: How to continue

    return data;
}

EDIT

If there is a better way to get the data let me know, I am not restricted to use a struct.

Upvotes: 1

Views: 343

Answers (1)

rustyx
rustyx

Reputation: 85266

Solution 1 (portable)

Read the header into a byte vector and convert each value one-by-one.

For example:

// read the header
std::vector<unsigned char> vec(26);
fh.read((char*)&vec.data(), vec.size());

data.Ny = vec[18] | (vec[19] << 8);
data.Nx = vec[20] | (vec[21] << 8);
data.L = vec[22];
data.distance = vec[23] | (vec[24] << 8);
. . .

Solution 2 (x86 only)

x86 is little-endian and not sensitive to data alignment, so we can read directly into a (packed) struct:

#include <cstdint>

#pragma pack(push, 1) // to prevent padding inside the struct
struct Header {
    uint8_t id;
    char date[17];
    uint16_t Ny;
    uint16_t Nx; 
    uint8_t L;
    uint16_t distance;
    uint8_t N;
};
#pragma pack(pop)

struct Data {
    Header hdr;
    std::vector<int16_t> quant_levels;
    std::vector<std::vector<unsigned char>> pixels; // [level][y][x]
};

Data readFile(const char* filename)
{
    Data data{};
    // open the file:
    std::fstream fh(filename, std::fstream::in | std::fstream::binary);

    // read the header
    fh.read((char*)&data.hdr, sizeof(data.hdr));

    data.quant_levels.resize(data.N);
    fh.read((char*)data.quant_levels.data(), data.N * 2);

    data.pixels.resize(data.L);
    for (auto& level : data.pixels) {
        level.resize(data.Nx * data.Ny);
        fh.read((char*)level.data(), data.Nx * data.Ny);
    }

    return data;
}

Note that there is a typo in your spec, bytes 1-7 should probably be 1-17 (note the next value starts at 18 and also 31-Mar-1998 11:55 is longer than 7 bytes).

Upvotes: 1

Related Questions