473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to read binary data into a STRUCT?

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.

--------------------------------
From: Michael Van Altena

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>7dFE/rBp9kufEMK5ukIchQ==</Id>
Jul 21 '05 #1
3 4558
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>
Jul 21 '05 #2
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" <an*******@dotnet247.com> wrote in message
news:OW**************@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>
Jul 21 '05 #3
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" <de****@oslokb.no> wrote in message
news:F9*******************@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" <an*******@dotnet247.com> wrote in
message news:OW**************@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>

Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Albert Tu | last post by:
Dear there, We have an x-ray CT system. The acquisition computer acquires x-ray projections and outputs multiple data files in binary format (2-byte unsigned integer) such as projection0.raw,...
6
by: someone | last post by:
Suppose that I have a class in an assembly that is delivered to the user, what can I do to change the class so that it doesn't break the binary compatibility? That is, user application can run...
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
0
by: sangui | last post by:
Helllo. this is biginner programmer. Would u check the file that I programed on c#(winform)? I tryed to make the program reading the binary file by C# programming but I failed. If u have...
3
by: Michael Van Altena via .NET 247 | last post by:
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...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
1
TMS
by: TMS | last post by:
I'm trying to write an address book that is based on a binary tree. I'm devloping in Visual C++ (I blew up my Ubuntu with the new dist, so no EMACS), starting with the basics: #ifndef...
5
by: Jens | last post by:
Hello, I have been looking for some C-code which listens on a user-defined port for incoming data traffic. When data is received, the data is written to a file. I found some C-code (server)...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.