Re: How to read binary data into a STRUCT?

From: Dennis Myrén (dennis_at_oslokb.no)
Date: 09/20/04


Date: Mon, 20 Sep 2004 11:12:53 +0200

And of course the "buff" byte array has to be filled with the appropriate
data.

-- 
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Dennis Myrén" <dennis@oslokb.no> wrote in message 
news:F9x3d.8048$WW4.124390@news4.e.nsc.no...
> byte [] buff = new byte [Marshal.SizeOf(STRUCT)];
> IntPtr ptr = Marshal.AllocHGlobal(buff.Length);
> Marshal.Copy(buff, 0x0, ptr, buff.Length);
> STRUCT result = (STRUCT) Marshal.PtrToStructure(ptr, typeof(STRUCT));
> Marshal.FreeHGlobal(ptr);
>
> [ StructLayout( LayoutKind.Sequential, Pack = 0x1 ) ]
> struct STRUCT
> {
> public ushort field1;
> [ MarshalAs( UnmanagedType.ByValArray, SizeConst = 0x3C ) ]
> public byte [] field2;
> ...
> }
>
> -- 
> Regards,
> Dennis JD Myrén
> Oslo Kodebureau
> "Michael Van Altena via .NET 247" <anonymous@dotnet247.com> wrote in 
> message news:OWJ957rnEHA.3428@TK2MSFTNGP11.phx.gbl...
> I'm trying to figure out how to read a formatted binary file into a 
> structure definition in C#. I've tried using the "StructLayout" attribute 
> with both LayoutKind.Explicit and LayoutKind.Sequential options.  I can 
> get this to work successfully, but only when I'm NOT dealing with arrays 
> in the structure definition.
>
> For example: In C, you are able to simply read a binary file into a 
> structure as follows:
>
> ---------------------------------------------------------------------------------------------------------
> /* The structure definition */
> typedef struct
> {
>  short anShorts[5];
>  char strString[32];
>  double adDoubles[10];
> } FORMATDEF;
>
> /* Open the file */
> FILE * pFile;
> pFile = fopen("Test.bin", "rb");
>
> /* Read into the structure */
> FORMATDEF structFormatDef;
> fread(pFile, &structFormatDef, sizeof(FORMATDEF));
>
> /* Close the file */
> fclose(pFile);
> ---------------------------------------------------------------------------------------------------------
>
> Note: The above example is using arrays in the STRUCT definition. This is 
> the main root of the problem I am trying to solve. (i.e. this is like the 
> StructLayout attribute with the LayoutKind.Sequential option (Pack=8 in my 
> particular problem) - but I can't get this to work with arrays in the 
> structure definition in C#).
>
> In C++ another possibility would be to use a UNION definition instead of a 
> class to union a structure definition with an array of characters (bytes), 
> and then just read in the bytes and access the data through the structure. 
> (i.e. this is like the StructLayout attribute with the LayoutKind.Explicit 
> option - but I can't get this to work with arrays in the structure 
> definition in C#).
>
> I can't believe with all the great features that C# has to offer, that 
> there isn't a simple way to read in a binary file into a formatted 
> structure and/or class.  Can someone out there tell me what I'm missing?
>
> Thanks,
>
> Michael.
>
> --------------------------------
> From: Michael Van Altena
>
> -----------------------
> Posted by a user from .NET 247 (http://www.dotnet247.com/)
>
> <Id>7dFE/rBp9kufEMK5ukIchQ==</Id>
> 


Relevant Pages

  • Re: How to read binary data into a STRUCT?
    ... I'm trying to figure out how to read a formatted binary file into a ... structure definition in C#. I've tried using the "StructLayout" attribute ... FORMATDEF structFormatDef; ... The above example is using arrays in the STRUCT definition. ...
    (microsoft.public.dotnet.general)
  • Re: How to read binary data into a STRUCT?
    ... I'm trying to figure out how to read a formatted binary file into a ... structure definition in C#. I've tried using the "StructLayout" attribute ... FORMATDEF structFormatDef; ... The above example is using arrays in the STRUCT definition. ...
    (microsoft.public.dotnet.general)
  • Re: binary file
    ... a text file and converting the text data obtained into a binary file. ... You've declared two arrays, each of which has only one element. ... you can pass them to fwrite. ...
    (comp.lang.c)
  • Re: Copy portions of array
    ... binary file. ... When compiled VB is astonishingly fast at dealing with Byte Arrays, ... discovered this when I wrote a Delphi DLL for sorting large files. ... I gave the DLL an option to CallBack to VB to perform the comparison, ...
    (microsoft.public.vb.general.discussion)
  • How to save C++ objects using Cocoa?
    ... I need to save arrays of custom C++ class objects in to a binary file. ... I am having to use C++ containers for cross compatibility. ...
    (comp.sys.mac.programmer.help)

Loading