reinterpretcast help . from struct to stream and vice-versa
I have a stream to store and retrieve values from - to an structure
Code:
My_structure {
int A;
double B;
};
put (My_structure &value) {
const char * buffer ;
buffer = reinterpret_cast<const char*> (&value);
the_stream.write(buffer,sizeof value);
}
get (My_structure & value) {
char * buffer ;
buffer = reinterpret_cast<char*> (&value);
the_stream.get(buffer, sizeof value);
value = *reinterpret_cast<My_structure*> (buffer);
}
This compile but I get strange values.... (the tellg and tellp are well pointed to zero before starts the get's)
I dont know how to write this last line (value = ) to get the right values...
Any help would be appreciated.
Re: reinterpretcast help . from struct to stream and vice-versa
the last line (value =), is not required, the_stream.get() will update the buffer (I assume so), as buffer is already pointing to value, value is updated indirectly (by updating buffer), unless you change buffer in side the_stream.get().
Re: reinterpretcast help . from struct to stream and vice-versa
One problem is on the line the_stream.get(buffer, sizeof value); -- sizeof value returns the lenght of a reference variable, not the size of the structure.
Re: reinterpretcast help . from struct to stream and vice-versa
Re: reinterpretcast help . from struct to stream and vice-versa