Reputation: 2785
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
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