Reading Structures from binary file

M

Matt Laver

Hi,

I have a binary file that I'm currently reading byte by byte using code
similiar to:

string FileName = @"c:\myFile.dat";
FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);

BinaryReader r = new BinaryReader(fs);

Header = r.ReadInt32(); //file header
Version = r.ReadInt32(); //file Version
FileSize = r.ReadInt32(); //file Size
....

Is there anyway that I could group some of these bytes into a structure
and read it in one go, e.g.:

public struct FirstStructure
{
public long header;
public long Version;
public long FileSize;
...

}

I've had a look at serialization, however have only found examples for
objects, not structures. I can not change the format of the file as it is
legacy and is written by comitting a series of records (delphi records)
to this file.

Anyone got any ideas or suggestions?

Thanks
 
B

Bob Powell [MVP]

The way I've done this before is to create an object with a constructor that
accepts a filestream. Then you just use the binary read to expand the data
into the structure.

This code is from a true-type fon reader I wrote...

The BigendianBinaryReader is just a derived binary reader. The principles
the same.
public CmapType0Table(Stream s)

{

BigendianBinaryReader br = new BigendianBinaryReader(s);

format=br.ReadUInt16();

length=br.ReadUInt16();

language=br.ReadUInt16();

glyphIndexArray=br.ReadBytes(256);

}


--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm
 
M

Mark Oliver

Hi, It would be nice if C# had a little better support for legacy data.
In just as ugly way in my application I used a constructor with a byte array
as a parameter:

internal static Encoding encodingASCII=System.Text.Encoding.ASCII;

public dfasu(byte [] record)

{

intAt22=Convert.ToInt32(encodingASCII.GetString(record,22,4));

longAt22=Convert.ToInt64(encodingASCII.GetString(record,22,12));

intAt34=Convert.ToInt32(encodingASCII.GetString(record,34,9));

..

..

..

}

Mark
 
W

William Stacey

It kinda does, but you need to use unsafe code (so you don't need the
Convert stuff). This example is network related, but the idea applies to
anything you can get into a byte[]. Naturally you may need to coerce the
fields after your populate your struct (i.e. endian conversions, etc.)
http://www.c-sharpcorner.com/Network/SimpleSnifferInCSLM.asp

--
William Stacey, DNS MVP

Mark Oliver said:
Hi, It would be nice if C# had a little better support for legacy data.
In just as ugly way in my application I used a constructor with a byte array
as a parameter:

internal static Encoding encodingASCII=System.Text.Encoding.ASCII;

public dfasu(byte [] record)

{

intAt22=Convert.ToInt32(encodingASCII.GetString(record,22,4));

longAt22=Convert.ToInt64(encodingASCII.GetString(record,22,12));

intAt34=Convert.ToInt32(encodingASCII.GetString(record,34,9));

.

.

.

}

Mark



Bob Powell said:
The way I've done this before is to create an object with a constructor that
accepts a filestream. Then you just use the binary read to expand the data
into the structure.

This code is from a true-type fon reader I wrote...

The BigendianBinaryReader is just a derived binary reader. The principles
the same.
public CmapType0Table(Stream s)

{

BigendianBinaryReader br = new BigendianBinaryReader(s);

format=br.ReadUInt16();

length=br.ReadUInt16();

language=br.ReadUInt16();

glyphIndexArray=br.ReadBytes(256);

}


--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top