If I store a float in a file via this code
fwrite((void*)(&v), sizeof(v), 1, f); // v is a float.
how often will a program reading the file with this code cause a runtime error because float is 8 bytes instead of 4?
float v;
fread((void*)(&v), sizeof(v), 1, f);
return v;
Can I always read 4 bytes and cast that to an 8 byte float? Would that be more portable?
Emphasis on different Windows Platforms 64 bit vs 32 bit.
I would be less worried about the size of the float and more worried about the endianness of it. I’d say the vast majority of C++ implementation use IEEE 754 which would mean float is always going to be 32 bits and double 64 bits.
You may wish to just serialize a text representation of the value, or else take particular care to make sure that the byte order is correct.