Re: How to read binary data into a STRUCT?
From: Cor Ligthert (notfirstname_at_planet.nl)
Date: 09/20/04
- Next message: Cor Ligthert: "Re: asp.net dynamic hyperlink"
- Previous message: Cor Ligthert: "Re: IUSR_machinename vs ASPNET"
- In reply to: Michael Van Altena via .NET 247: "How to read binary data into a STRUCT?"
- Next in thread: Dennis Myrén: "Re: How to read binary data into a STRUCT?"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 20 Sep 2004 08:05:45 +0200
Michael,
Did you know that there is a newsgroup
microsoft.public.dotnet.languages.csharp
There is a possibility you get quicker an answer there,
Cor
"Michael Van Altena via .NET 247"
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>
- Next message: Cor Ligthert: "Re: asp.net dynamic hyperlink"
- Previous message: Cor Ligthert: "Re: IUSR_machinename vs ASPNET"
- In reply to: Michael Van Altena via .NET 247: "How to read binary data into a STRUCT?"
- Next in thread: Dennis Myrén: "Re: How to read binary data into a STRUCT?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|