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.