reading binary files
-
I am trying to write a C# program to read a binary file. The records have a predefined strucutre. Problem is I have no idea to to define the struct in C# in the way I did in C++. I also need the strucutre to be in 1 byte alignment. In C++ the structure looks as follows: #pragma pack(1) struct my_struct_ { unsigned char fld1[10]; long fld2; short fld3; }; #pragma pack() I tried the MSDN but the examples looks so complicated I can't understand a thing.. :-(
-
I am trying to write a C# program to read a binary file. The records have a predefined strucutre. Problem is I have no idea to to define the struct in C# in the way I did in C++. I also need the strucutre to be in 1 byte alignment. In C++ the structure looks as follows: #pragma pack(1) struct my_struct_ { unsigned char fld1[10]; long fld2; short fld3; }; #pragma pack() I tried the MSDN but the examples looks so complicated I can't understand a thing.. :-(
-
Thanks for that. However, I still don't know to force 1 byte strucutre member alignment and array of bytes?
-
Thanks for that. However, I still don't know to force 1 byte strucutre member alignment and array of bytes?
You can declare an array of bytes like this:
byte[]
buffer
= new byte[1];Regards, Polis Can you practice what you teach?
-
Thanks for that. However, I still don't know to force 1 byte strucutre member alignment and array of bytes?
Packing size is done as follows:
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct WhoCares
{
public int x;
public int y;
}RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
You can declare an array of bytes like this:
byte[]
buffer
= new byte[1];Regards, Polis Can you practice what you teach?
Thanks, but I am not sure that's what I need. In C++, my structure looks as follows: struct sample_ { unsigned short f1; long f2; unsigned char filler[1000]; }; // total 1006 I declared it in C# as follows: [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=1)] public class sample_ { public ushort f1; public int f2; public byte[] arr=new byte[1000]; } But it's not good because the size should 1006, and it is actually 10 (2+4+4). sample_ mySample = new sample_(); Console.WriteLine ("sizeof={0}", Marshal.SizeOf(mySample)); Is there a way I can declare the array without new? Or do I have to declare 1000 byte fillers?
-
Packing size is done as follows:
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct WhoCares
{
public int x;
public int y;
}RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
I got the same problem.at last ,I found a question that the size of the struct is very complex as we thought.But there is a way to read the blok of memory that stored as struct type.Maybe we can disguss it together.
-
I got the same problem.at last ,I found a question that the size of the struct is very complex as we thought.But there is a way to read the blok of memory that stored as struct type.Maybe we can disguss it together.
Not until you start your own question as a new thread. I'm not responding to someone who thinks they can just hyjack someone elses 4 year old thread and I'l magically know what they're talking about.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...