Reading structures from a binary file

  • Thread starter Thread starter John Dann
  • Start date Start date
J

John Dann

I'm trying to read some binary data from a file created by another
program. I know the binary file format but can't change or control the
format. The binary data is organised such that it should populate a
series of structures of specified variable composition.

I have the structures created OK, but actually reading the files is
giving me an error. Can I ask a simple question to start with:

I'm trying to read the file using the deserialize method. Since the
original file wasn't created with serialize am I wasting my time
trying to do this?

If I can use deserialize in this situation then I'll perservere with
looking for the error, but first I'd like to check that the approach
is valid in principle.

TIA
JGD
 
* John Dann said:
I'm trying to read some binary data from a file created by another
program. I know the binary file format but can't change or control the
format. The binary data is organised such that it should populate a
series of structures of specified variable composition.

I have the structures created OK, but actually reading the files is
giving me an error. Can I ask a simple question to start with:

Try to read the file using 'FileGet'.
 
John,
In addition to the other comments.

Is the other program a .NET program or VB6 or something else?

If its VB6 or something else, I would consider using BinaryReader &
BinaryWriter to do my own binary serialization of the structures.

The following article offers using a BinaryReader & BinaryWriter to
serializing a class to a NetworkStream, you can use a FileStream and have
the same effect.

http://msdn.microsoft.com/library/default.asp?url=/library/en-s/dncscol/html/csharp09182003.asp

The example is in C#, however it should be easily converted to VB.NET, post
if you need help.

Hope this helps
Jay
 
Try to read the file using 'FileGet'.

I take it that this means that Deserialize won't work in this
situation, which I can understand if Serialize adds some extra
information to the binary file beyond its basic data content to help
in reading back via Deserialize. So for my erudition is this actually
the case?

One other comment: I think I used to use FileGetfor this purpose in a
previous VB6 incarnation of a related program. I was thinking I ought
to get more used to the (?) preferred .net way of doing things hence
why I was looking at deserialize and then failing that I was going to
try a binaryreader approach. But you seem to be implying that the
particular situation I'm tackling (ie reading structures from a
pre-existing binary file) is not best handled by one of the newer .net
input methods and FileGet is as good as anything despite being, well,
say older technology. Have I read this correctly?

JGD
 
John,
What do you mean by "Serialize" and "Deserialize".

If you used Binary or XML Serialization to Serialize the file, then you
should use Binary or XML Serializaiton to Deserialize the file respectively.

Note that its best if the classes that are being serialized & deserialized
as When you serialize an object to a file, one of the items serialized is
the assembly doing the serialization, when you deserialize the object, the
system checks the current assembly version against the version in the
serialized object. If these versions are different you see the error.

I would recommend you use a single class library that does the actual
serialization & deserialization, ensuring the assembly version is constant.

One of the following articles on serialization discusses how you can change
the version info read from an assembly, which is used more for version 2
program being able to read version 1 versions of the serialized data, not so
program 1 can read program 2 data per se (read, as I would recommend a
single assembly used by both programs).

http://msdn.microsoft.com/msdnmag/issues/02/04/net/
http://msdn.microsoft.com/msdnmag/issues/02/07/net/
http://msdn.microsoft.com/msdnmag/issues/02/09/net/

Hope this helps
Jay
 
Try to read the file using 'FileGet'.

Actually, thinking about this, isn't FileGet just for random access
files, ie where the file consists of repeated records _all_ of the
same size. Whereas the situation I have, as per the original post, is
one where the records are not all of the same size. (In fact, they are
all of the same size except that the first record/structure is header
data and is of different size to the others. But I think this one
exception is enough to break the general approach of using FileGet.)

JGD
 
Jay, many thanks for your responses:
Is the other program a .NET program or VB6 or something else?

The other program is a proprietary program written I think in C. The
only information I have about it, beyond user documentation, is the
format of the binary file, which is described in terms of C
structures. I have previously read (an older version of) this file
successfully using VB6, so wasn't anticipating any problem with
VB.net[1].

However, I wasn't sure which of the newer .net IO routines to use.
Looking at a couple of books seemed to suggest that deserialize was a
good and efficient approach to reading structures from a binary file.
But what was never really clear was whether the binary file needed to
be created by serialize before it could be read by deserialize or
whether deserialize could be used with _any_ binary file of known
format, whatever its provenance. AFAICS the answer to the latter idea
now seems to be a definite no. In other words, serialize seems to add
additional information to the binary file beyond the actual binary
structures, information which is needed before the file can be read by
deserialize.

Maybe this is obvious to anyone who's worked with de/serialize before,
but it wasn't a point brought out in any of the books I had to hand.

Yes I'll certainly try BinaryReader as a next step, though I'm
slightly concerned that performance for reading thousands of records
may be an issue. But we'll see.

Thanks again.
JGD

[1] Actually there is one potential issue in a newer version of the
file format in that one parameter is saved as an unsigned short. I can
see that there's a vb.net method to read unsigned shorts from a
binary file, but I'm not quite sure how to handle it thereafter. But
that's perhaps a subject for a new thread if necessary.
 
John,
The other program is a proprietary program written I think in C. The
only information I have about it, beyond user documentation, is the
format of the binary file, which is described in terms of C
structures. I have previously read (an older version of) this file
successfully using VB6, so wasn't anticipating any problem with
VB.net[1].
I would use either a BinaryReader or a Stream & System.BitConverter. Seeing
as I would expect the BinaryReader is implemented in terms of
System.BitConverter or something lower I would use the BinaryReader and not
bother reimplementing the logic... Especially where my reimplementation may
not be as efficient as the BinaryReader...

I would follow the pattern identified in the article I posted earlier.
Yes I'll certainly try BinaryReader as a next step, though I'm
slightly concerned that performance for reading thousands of records
may be an issue. But we'll see.
If the BinaryReader was a performance problem, then I would use a different
language (like C or C++ itself). As the BinaryReader is as "low level" as
you are going to get, short of using Win32 API directly.

In other words I would not expect the BinaryReader itself to be the
performance problem per se, but I could see mis-using the data returned from
the BinaryReader may be a performance problem.
[1] Actually there is one potential issue in a newer version of the
file format in that one parameter is saved as an unsigned short. I can
see that there's a vb.net method to read unsigned shorts from a
binary file, but I'm not quite sure how to handle it thereafter. But
that's perhaps a subject for a new thread if necessary.
VB.NET does not really support unsigned Ints, I would read them as Signed
Ints, unless there is a chance they would be greater the Integer.MaxValue.
You can use BinaryReader to read the unsigned integer, however VB.NET does
not yet really allow you to do anything with it. No operators allowed, only
calling methods that return or accept an unsigned int as a parameter...

The one caveat to watch for with the BinaryReader is how strings are
encoded. BinaryReader.GetString expects length prefixed strings in the
System.Text.Encoding specified on the BinaryReader's constructor. The number
of bytes used for the length is variable length. If you have fixed length
strings or null terminated strings in the file, I would suggest a helper
function reading the string into a byte array, then using
System.Text.Encoding.GetString to convert this byte array into a String.

Hope this helps
Jay

John Dann said:
Jay, many thanks for your responses:
Is the other program a .NET program or VB6 or something else?

The other program is a proprietary program written I think in C. The
only information I have about it, beyond user documentation, is the
format of the binary file, which is described in terms of C
structures. I have previously read (an older version of) this file
successfully using VB6, so wasn't anticipating any problem with
VB.net[1].

However, I wasn't sure which of the newer .net IO routines to use.
Looking at a couple of books seemed to suggest that deserialize was a
good and efficient approach to reading structures from a binary file.
But what was never really clear was whether the binary file needed to
be created by serialize before it could be read by deserialize or
whether deserialize could be used with _any_ binary file of known
format, whatever its provenance. AFAICS the answer to the latter idea
now seems to be a definite no. In other words, serialize seems to add
additional information to the binary file beyond the actual binary
structures, information which is needed before the file can be read by
deserialize.

Maybe this is obvious to anyone who's worked with de/serialize before,
but it wasn't a point brought out in any of the books I had to hand.

Yes I'll certainly try BinaryReader as a next step, though I'm
slightly concerned that performance for reading thousands of records
may be an issue. But we'll see.

Thanks again.
JGD

[1] Actually there is one potential issue in a newer version of the
file format in that one parameter is saved as an unsigned short. I can
see that there's a vb.net method to read unsigned shorts from a
binary file, but I'm not quite sure how to handle it thereafter. But
that's perhaps a subject for a new thread if necessary.
If its VB6 or something else, I would consider using BinaryReader &
BinaryWriter to do my own binary serialization of the structures.

The following article offers using a BinaryReader & BinaryWriter to
serializing a class to a NetworkStream, you can use a FileStream and have
the same effect.

http://msdn.microsoft.com/library/default.asp?url=/library/en-s/dncscol/htm l/csharp09182003.asp

The example is in C#, however it should be easily converted to VB.NET, post
if you need help.

Hope this helps
Jay
 
Back
Top