Re: Interesting problem: serializing in with MFC CArchive, reading out using C# BinaryReader

From: Scott McPhillips [MVP] (org-dot-mvps-at-scottmcp)
Date: 11/11/04


Date: Thu, 11 Nov 2004 18:51:03 -0500

RP2001 wrote:

> I've serialized in various variables of various types (mainly CString,
> int, double) into an instantiated MFC CArchive class and saved it as a
> binary file.
>
> I am able to open the binary file and read out the individual
> variables fine with an MFC application. However I cannot read the
> binary file correctly with a C#.NET application, where I'm using the
> BinaryReader Class. Doing some debugging I noticed that the problem
> occured at this C# line of code:
>
> my_variable_of_type_CString =
> instantiated_CSharp_binaryreader.ReadString();
>
> When this line of code execute, the binary reads out the ENTIRE
> remainder of the file. So I thought is this one of those hairy, good
> old string termination issues, but I've checked the byte representing
> the string termination and it's there.
>

Actually, a CString in a CArchive file does not have a terminator. You
are assuming that the C# ReadString knows how MFC encodes a string in a
CArchive. I don't know what C# does, but this seems a very unlikely
assumption. CArchive prefixes the string with its length, and the
length of the length field is variable and binary. So you have a lot
more work to do - translating the CArchive code - before you can expect
to properly interpret a binary image of a CArchive.

-- 
Scott McPhillips [VC++ MVP]


Relevant Pages

  • Re: Interesting problem: serializing in with MFC CArchive, reading out using C# BinaryReader
    ... >> I am able to open the binary file and read out the individual ... >> variables fine with an MFC application. ... >> BinaryReader Class. ... >> old string termination issues, but I've checked the byte representing ...
    (microsoft.public.vc.mfc)
  • Re: Reading structures from a binary file
    ... Looking at a couple of books seemed to suggest that deserialize was a ... good and efficient approach to reading structures from a binary file. ... In other words, serialize seems to add ... Yes I'll certainly try BinaryReader as a next step, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Reading structures from a binary file
    ... I would use either a BinaryReader or a Stream & System.BitConverter. ... If the BinaryReader was a performance problem, then I would use a different ... > binary file, but I'm not quite sure how to handle it thereafter. ... The one caveat to watch for with the BinaryReader is how strings are ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Interesting problem: serializing in with MFC CArchive, reading out using C# BinaryReader
    ... I solved it by looking into the MFC ... public string ReadCString() ... >> I am able to open the binary file and read out the individual ... >> old string termination issues, but I've checked the byte representing ...
    (microsoft.public.dotnet.languages.csharp)