reading floats in a struct
-
Hi all, I have a struct a bit like this, which is the header to a file on disk:-
struct Header
{
short a;
short b;
short c;
float floatvalue;
}I read the header from the disk like this:-
ReadFile(hFile,&header,sizeof(Header),&bytesRead,NULL);
Once I'd done that, the values in a,b,c are as expected, but the floatvalue is not. However, if I adjust the struct so that instead of float, I have BYTE[4] as follows:-
struct Header
{
short a;
short b;
short c;
BYTE floatvalue[4];
}And then after reading the header as before, I do:-
float f; memcpy(&f,header.floatvalue,sizeof(float));
Then f contains the value expected. I don't understand why the first version didn't work. Can anyone enlighten me please? Thanks Jon
using System.Beer;
-
Hi all, I have a struct a bit like this, which is the header to a file on disk:-
struct Header
{
short a;
short b;
short c;
float floatvalue;
}I read the header from the disk like this:-
ReadFile(hFile,&header,sizeof(Header),&bytesRead,NULL);
Once I'd done that, the values in a,b,c are as expected, but the floatvalue is not. However, if I adjust the struct so that instead of float, I have BYTE[4] as follows:-
struct Header
{
short a;
short b;
short c;
BYTE floatvalue[4];
}And then after reading the header as before, I do:-
float f; memcpy(&f,header.floatvalue,sizeof(float));
Then f contains the value expected. I don't understand why the first version didn't work. Can anyone enlighten me please? Thanks Jon
using System.Beer;
-
Thank for that, it's clear now. Looks like a can get around the issue with #pragma pack (2)
using System.Beer;
-