Re: Writing binary data in managed C++

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Lord2702 (Lord2702_at_MSN.com)
Date: 10/02/04


Date: Sat, 2 Oct 2004 15:08:43 -0700

You can write your struct in BinaryFormat. But if you expect it should write
as you use to write in Native C or C++ with fopen(), and fwrite(), or
open(), and write(), functions then you can not. But you can use
BinaryWriter class, it has many overloaded methods of write, which can write
your struct. Make two methods in your struct Read and Write, or Load and
Save as per your choice, and write all the types with by passing
BinaryWriter instance to these methods. This will not write any headers by
the way. for example. string: S"ABCDE", will write 05, 41, 42, 43, 44, 45
(all in hex code) in Bytes. first byte is the length of your string. Int32
will write four bytes.

Good Luck.

"Peter Steele" <psteele@z-force.com> wrote in message
news:OvgAPS0pEHA.1688@TK2MSFTNGP10.phx.gbl...
> I have a managed C++ project that contains a simple structure:
>
> struct
> {
> <multiple members>
> } mydata;
>
> I want to write the contents of this structure out to disk retaining the
> exactly binary data without any additional header information, and of
course
> be able to later read it back in. What's the best way to accomplish this?
>
>



Relevant Pages

  • Re: memcpy functionality? please help!
    ... Using BinaryWriter and Encoding.GetBytes will do the trick, and based on the encoding you have/need the string on the other side, you can choose the right one in .NET. ... I will need to be able to do this for a struct with mixed types. ... public int iOne; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Sending a struct using TcpClient or UdpClient
    ... MyMessage msg = new MyMessage; ... BinaryWriter wr = new BinaryWriter; ... > I'm trying to send a struct using ... TcpClient or UdpClient. ...
    (microsoft.public.dotnet.framework)
  • Re: how to store list of varying types
    ... When there's a variable-length string, ... typedef struct { ... pointer null, and the second one the CString object. ... then have to finish constructing the packet by copying the two data objects ...
    (microsoft.public.vc.mfc)
  • Re: MVC in C++
    ... Things are now flowing more along the classic MVC lines. ... > struct Observable ... > string version; ... In that case the Controller would trigger a model update *and it* could ...
    (comp.object)
  • A note on personal corruption as a result of using C
    ... Many people in this ng are personally corrupt and use this ng to take ... "A string cannot contain Nuls" Yes it can. ... "A struct is a class" No, ... It appears that no C maven has read a history of mathematics. ...
    (comp.programming)