473,714 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading structures from a binary file

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
Nov 20 '05 #1
7 6061
* John Dann <ne**@prodata.c o.uk> scripsit:
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'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #2
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/de...rp09182003.asp

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

Hope this helps
Jay

"John Dann" <ne**@prodata.c o.uk> wrote in message
news:03******** *************** *********@4ax.c om...
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

Nov 20 '05 #3
On 24 Jun 2004 17:48:28 +0200, hi************* **@gmx.at (Herfried K.
Wagner [MVP]) wrote:
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
Nov 20 '05 #4
John,
What do you mean by "Serialize" and "Deserializ e".

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
"John Dann" <ne**@prodata.c o.uk> wrote in message
news:4d******** *************** *********@4ax.c om...
On 24 Jun 2004 17:48:28 +0200, hi************* **@gmx.at (Herfried K.
Wagner [MVP]) wrote:
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

Nov 20 '05 #5
On 24 Jun 2004 17:48:28 +0200, hi************* **@gmx.at (Herfried K.
Wagner [MVP]) wrote:

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
Nov 20 '05 #6
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/de...rp09182003.asp

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

Hope this helps
Jay

"John Dann" <ne**@prodata.c o.uk> wrote in message
news:03******* *************** **********@4ax. com...
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


Nov 20 '05 #7
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.BitConve rter. Seeing
as I would expect the BinaryReader is implemented in terms of
System.BitConve rter or something lower I would use the BinaryReader and not
bother reimplementing the logic... Especially where my reimplementatio n 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.MaxValu e.
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.Ge tString expects length prefixed strings in the
System.Text.Enc oding 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.Enc oding.GetString to convert this byte array into a String.

Hope this helps
Jay

"John Dann" <ne**@prodata.c o.uk> wrote in message
news:u8******** *************** *********@4ax.c om... 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/de...-s/dncscol/htm

l/csharp09182003. asp

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

Hope this helps
Jay

"John Dann" <ne**@prodata.c o.uk> wrote in message
news:03******* *************** **********@4ax. com...
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

Nov 20 '05 #8

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

Similar topics

3
8376
by: Matt Laver | last post by:
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);
1
3039
by: Manjunath sp via DotNetMonster.com | last post by:
Hi, How to effectively write and read structures from binary files in .Net? Currently I am using functions like ReadInt32 and the likes to read data from binary file into each elements of a structure, but this is very inefficent compared to VB3 where I could use Get method to read the whole structure data? Is there any similar method in .net? -- Message posted via http://www.dotnetmonster.com
3
2461
by: Usenet User | last post by:
I am trying to read (and then save) a binary file which has certain data structures in it. (The file is in propritetary format produced by a 3rd party MFC application.) I know that those data structures would correspond to the structures similar to the one below: private struct MyStruct {
23
25489
by: Graham F French | last post by:
Hello, I can read text files into my application, but I cannot read in msg files as they seem to be in a proprietry format. Is there anyway of converting it on the fly or is there an interpretation method? Thanks
3
3437
by: Zeke Zinzul | last post by:
Hi Guys & Geeks, What's the most elegant way of dealing with binary data and structures? Say I have this (which I actually do, a woo-hoo): struct Struct_IconHeader { byte width; byte height;
6
5269
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
13
3705
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 consists of structures of type---- struct record { int acountnum; char name; float value;
4
1504
by: Mastastealth | last post by:
I'm trying to create a program to read a certain binary format. I have the format's spec which goes something like: First 6 bytes: String Next 4 bytes: 3 digit number and a blank byte --- Next byte: Height (Number up to 255) Next byte: Width (Number up to 255) Next byte: Number 0 - 5 Every 2 bytes after that: Supposedly a number 0000 - 0899?
3
11153
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I'm trying to write an array of structures named myStructArray to a binary file and later on read it back. Although I could complete the entire project in C in about 2 minutes, I obviously have my head up and locked when it comes to C#. My first attempt to read such a file was something like: myBinaryReader.ReadBytes(sizeof(myStructArray));
0
9314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9015
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7953
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4464
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4725
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3158
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2110
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.