Reputation: 1191
I have a binary file that I am trying to extract data from. The last 5 data points int the file are 10 bit integer types and I am struggling on how to extract that information and convert it into something readable. I have tried the following code:
struct bitField
{
unsigned value: 10;
};
struct Data
{
bitField x;
}
int main()
{
std::array<char,696> buffer;
std::ifstream file ("file.bin", std::ios::in | std::ios::binary);
file.read(buffer.data(),buffer.size());
Data a;
std::memcpy(&a.x.value,&buffer[612],sizeof(struct bitField));
}
I am then met with the error attempt to take address of bit-field
. I have then tried using std::bitset<10>
in place of bitField in my Data struct. And while I do not get a compiler error, I get back a bunch of 0's instead which I believe is incorrect data.
How do you properly read in the data?
Upvotes: 0
Views: 2165
Reputation: 4099
You can't take an address of a bit-field value as it may not be byte-aligned. You should copy directly into a.x (not a.x.value).
Further, you don't really need to have a separate bitfield struct. You can simply put bitfields right into Data struct.
See this on how to use bitfields: https://www.geeksforgeeks.org/bit-fields-c/
Upvotes: 1