On Thu, 31 Jul 2003 21:41:28 -0500, lurkerboy <> wrote:
>I am reading in 4 bytes from a file into a char array, i.e., char
>holdme[4] . I need to cast these 4 bytes so I can read them as a
>float type.
Generally, read data back from the file the same way it was stored.
Presumably these four bytes weren't stored as four individual bytes,
but as a 'float' value.
Why don't you just read back that value _as_ a 'float' value?
>For example in VC++, debug, I can see that holdme = {0x10, 0xA2, 0x72,
>0x94}. I want to read those four bytes as a float variable. As I
>interpret the help manual, a float type is 4 bytes, but I'm getting
>cast error messages when I do something like this:
>
>int main()
>{
>char holdme[4];
>float myfloat;
>...
>*myfloat = four_chars2float(char holdme)
'myfloat' is not a pointer.
>...
>
>}
>
>
>float four_chars2long(char value[] )
This is equivalent to
float four_chars2long( char* value )
>{
> float *midwaypoint;
> midwaypoint = (float) &value;
The address of 'value' is the address of a pointer, which for your
purposes is meaningless.
The cast to 'float' is meaningless.
Finally, assigning a 'float' to a 'float*' pointer is meaningless.
> return *midwaypoint;
>}
>
>If there is better way, please show me as I am a rank beginner with
>VC++.
See above.
Also keep in mind that float is not necessarily 4 bytes with other
compilers.
If you control the creation of the file, I suggest you stick to text
files.
|