Click to See Complete Forum and Search --> : Not getting the needed result


Kohinoor24
June 24th, 2002, 08:23 AM
Iam writing into a buffer an object as bytes.
Then When Iam Trying to read from the buffer the object as say

out.write(m_pRead, BytesToRead);
where "m_pRead" is a pointer to the beginning of the buffer & "BytesToRead" is the no:of bytes to read.But Iam not getting the expected result in the return file.

But when I store the same thing in a list & iterate the list to a console output, Iam getting the desired result.

so why Iam not able to get the result when Iam reading the object as bytes

Thanks in advance

cup
June 24th, 2002, 01:16 PM
You are writing the data in bytes so that implies that it is in binary. Have you opened the file for writing in binary?

Writing it on the console implies that it is text. Are you trying to write binary data and read it in text form? Is that what is not working?

Kohinoor24
June 25th, 2002, 01:27 AM
Iam opening the file like this: & writing to it

ofstream out("File",ios::out);
out.write(m_pRead, BytesToRead);
out.close();

cup
June 25th, 2002, 02:54 AM
This depends on whether you are using fstream or fstream.h. I haven't found anything in fstream but in fstream.h, after the declaration of out, you can add

out.setmode(filebuff::binary);

cpitis
June 26th, 2002, 01:42 AM
Are you sure the object is POD (plain old data)? If the object contains pointers, the addresses of the pointers are preserved in the file. When loading, these pointers are not valid anymore.

Another problem when saving an object as binary memory block is when it has virtual methods. The VMT will also be saved. If you have RTTI enabled, the information will also be saved.

So, pointers, VMT and RTTI are not persistent. They are generated at run time and should not be stored.

Solution: try to save each member separately. Make methods for load and save and avoid to load and save the object as memory block.