CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2001
    Posts
    745

    Not getting the needed result

    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 nof 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

  2. #2
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    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?
    Succinct is verbose for terse

  3. #3
    Join Date
    Oct 2001
    Posts
    745

    the way

    Iam opening the file like this: & writing to it

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

  4. #4
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    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);
    Succinct is verbose for terse

  5. #5
    Join Date
    Feb 2002
    Posts
    81
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured