M
Michael Van Altena via .NET 247
I'm trying to figure out how to read a formatted binary file intoa structure definition in C#. I've tried using the"StructLayout" attribute with both LayoutKind.Explicit andLayoutKind.Sequential options. I can get this to worksuccessfully, but only when I'm NOT dealing with arrays in thestructure definition.
For example: In C, you are able to simply read a binary file intoa 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 theLayoutKind.Sequential option (Pack=8 in my particular problem) -but I can't get this to work with arrays in the structuredefinition in C#).
In C++ another possibility would be to use a UNION definitioninstead of a class to union a structure definition with an arrayof characters (bytes), and then just read in the bytes andaccess the data through the structure. (i.e. this is like theStructLayout attribute with the LayoutKind.Explicit option - butI can't get this to work with arrays in the structure definitionin 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 aformatted structure and/or class. Can someone out there tell mewhat I'm missing?
Thanks,
Michael.
For example: In C, you are able to simply read a binary file intoa 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 theLayoutKind.Sequential option (Pack=8 in my particular problem) -but I can't get this to work with arrays in the structuredefinition in C#).
In C++ another possibility would be to use a UNION definitioninstead of a class to union a structure definition with an arrayof characters (bytes), and then just read in the bytes andaccess the data through the structure. (i.e. this is like theStructLayout attribute with the LayoutKind.Explicit option - butI can't get this to work with arrays in the structure definitionin 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 aformatted structure and/or class. Can someone out there tell mewhat I'm missing?
Thanks,
Michael.