H
herobeat
Hi all,
I'm new to C Sharp programming, though a long time ago I was fairly
competent in C and C++.
I have data structures in a known format in binary files. I need to
be able to read them, preferably as easily as possible into a struct.
For example, I have a struct that looks something like this:
public struct MyStructure
{
public UInt32 MyNumber1;
public UInt32 MyNumber2;
public UInt32 MyNumber3;
...
}
In my code, I'd like to do something like this:
MyStructure s;
BinaryReader f =
new BinaryReader(File.Open("myfile.bin", FileMode.Open));
s = f.ReadBytes(sizeof s); // This doesn't work!!!
Obviously, that last night is heinously wrong in managed code, but I
don't know how to do what it is trying to do. What is the best way to
take binary data out of a file and get it into a structure in C
Sharp? Surely there's got to be something better than:
s.MyNumber1 = f.ReadUInt32();
s.MyNumber2 = f.ReadUInt32();
s.MyNumber3 = f.ReadUInt32();
....
On a side note, since I am still pretty new to structs as C Sharp
knows them, is this even the entity I should be using? If not, what
is?
Thanks for any advice!
I'm new to C Sharp programming, though a long time ago I was fairly
competent in C and C++.
I have data structures in a known format in binary files. I need to
be able to read them, preferably as easily as possible into a struct.
For example, I have a struct that looks something like this:
public struct MyStructure
{
public UInt32 MyNumber1;
public UInt32 MyNumber2;
public UInt32 MyNumber3;
...
}
In my code, I'd like to do something like this:
MyStructure s;
BinaryReader f =
new BinaryReader(File.Open("myfile.bin", FileMode.Open));
s = f.ReadBytes(sizeof s); // This doesn't work!!!
Obviously, that last night is heinously wrong in managed code, but I
don't know how to do what it is trying to do. What is the best way to
take binary data out of a file and get it into a structure in C
Sharp? Surely there's got to be something better than:
s.MyNumber1 = f.ReadUInt32();
s.MyNumber2 = f.ReadUInt32();
s.MyNumber3 = f.ReadUInt32();
....
On a side note, since I am still pretty new to structs as C Sharp
knows them, is this even the entity I should be using? If not, what
is?
Thanks for any advice!