Re: Writing binary data in managed C++

From: Peter Steele (psteele_at_z-force.com)
Date: 10/01/04


Date: Thu, 30 Sep 2004 19:25:51 -0700

John, here's what I came up with. Assuming we have a simple struct as
follows:

      struct

      {

        ...

      } X;

whose members have been initialized in some manner, then the following code
can be used to dump the bytes of this structure:

      FileStream* fs = new FileStream("C:\\data.bin", FileMode::Create,
FileAccess::Write);

      BinaryWriter* bw = new BinaryWriter(fs);

      char* bytes = (char*)&X;

      char gcbytes __gc[] = new char __gc[sizeof X];

      for (int i = 0; i < sizeof X; i++) gcbytes[i] = bytes[i];

      bw->Write(gcbytes);

      bw->Flush();

      bw->Close();

What I don't like about this solution is that it duplicates the entire
structure. Can the data be output directly without this duplication?

Peter

"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: LIVEDUMP
    ... I'll just edit an empty file and load the dump. ... John Keiser ... "Vowing he'd ne'er consent, consented". ... Of course if we are looking at arithmetic data, octal is the way to go. ...
    (comp.sys.unisys)
  • Re: at 3:30AM it hit!
    ... I took a crap. ... The dump of dumps. ... John H ... Not everybody is such a fat slob that they need mechanical help. ...
    (rec.boats)
  • Re: at 3:30AM it hit!
    ... harry, you already did. ... I took a crap. ... The dump of dumps. ... John H ...
    (rec.boats)
  • Re: How do I dump a database to a local file on Windows?
    ... >You need to dump the database to the Unix and then ftp to your PC. ... >For handling the File system from ASE, you need have a licence feature ... >of ASE which allows that. ... John Brock ...
    (comp.databases.sybase)
  • Re: lsqcurvefit
    ... John D'Errico wrote: ... I'm using lsqcurvefit to fit a model, I like to know if it's ... The best material model of a cat is another, ... You say to dump but it dump only the last step. ...
    (comp.soft-sys.matlab)