Leon palafox
Leon palafox

Reputation: 2785

Read binary data on C++ where different columns have different data types

We have a binary file that represents data arranged in columns.

Each column has a different data format, for example:

What would be the best way to read these files in C++, I can do it in matlab, but I don't really have much clue of how to do it in C++

Upvotes: 2

Views: 843

Answers (1)

DeathByTensors
DeathByTensors

Reputation: 941

Assuming that these values are in order:

unsigned long int dataMember0 = 0;
int dataMember1 = 0;
float dataMember2 = 0.0;

std::ifstream fileStream("file.bin", std::ios::in | std::ios::binary);
fileStream.read((char*)&dataMember0, sizeof(unsigned long int));
fileStream.read((char*)&dataMember1, sizeof(int));
fileStream.read((char*)&dataMember2, sizeof(float));

You cast a char pointer because it is being read as an array of bytes (char is one byte). If you want to loop this process: while(fileStream) {...} will excute until there is no more to read

Upvotes: 6

Related Questions