472,975 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,975 software developers and data experts.

Writing/Reading an array of structures to/from a binary file?

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));

Although using sizeof this way in C/C++ works fine, it results in a compiler
error in C#. After inspecting the error further it suggested using the
following, which then compiled fine:

myBinaryReader.ReadBytes(Marshal.SizeOf(myStructAr ray));

But then I had the problem of where to store the bytes read since the
ReadBytes method returns a byte array but I wanted to store them into my
structure array. I tried both

myStructArray= myBinaryReader.ReadBytes(Marshal.SizeOf(myStructAr ray));
and
(byte[])myStructArray=
myBinaryReader.ReadBytes(Marshal.SizeOf(myStructAr ray));

I didn't expect either to compile, and I was not disappointed.

------------------------------------

Later on I wanted to write the structure array back out to a binary file.
My first inclination was code similar to:

myBinaryWriter.Write(myStructArray);

but I did not actually expect it to compile since the Write method doesn't
have an overload for a generic "object" type.

Very soon I realized that I had no clue how to do it in C# and was obviously
badly missing the point somewhere. Could someone please give me a good swift
kick and enlighten me? I checked around the Web but only found simple
examples of reading/writing the primitive data types as binary, which was no
help.

Thanks,
Ray Mitchell

Aug 10 '08 #1
3 11064
Ray Mitchell <Ra*****************@MeanOldTeacher.comwrote:
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#.
First question: do you *really* want to have structures in the first
place? It's very rarely the correct design decision to create a struct
instead of a class.
My first attempt to read such a file was something like:

myBinaryReader.ReadBytes(sizeof(myStructArray));

Although using sizeof this way in C/C++ works fine
So long as you don't care about the portability of the data, of course.
Quick'n'dirty serialization like this is rarely a good idea even in
C/C++, in my view.

There are lots of ways of managing serialization in .NET. You could add
methods WriteToStream and ReadFromStream to your custom type for manual
serialization, or there are various alternatives for it to be more
automated.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 10 '08 #2
BinaryFormatter should work fine.

FileStream fs = new FileStream("DataFile.dat", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, somestruct);

"Ray Mitchell" <Ra*****************@MeanOldTeacher.comwrote in message
news:4F**********************************@microsof t.com...
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));

Although using sizeof this way in C/C++ works fine, it results in a
compiler
error in C#. After inspecting the error further it suggested using the
following, which then compiled fine:

myBinaryReader.ReadBytes(Marshal.SizeOf(myStructAr ray));

But then I had the problem of where to store the bytes read since the
ReadBytes method returns a byte array but I wanted to store them into my
structure array. I tried both

myStructArray= myBinaryReader.ReadBytes(Marshal.SizeOf(myStructAr ray));
and
(byte[])myStructArray=
myBinaryReader.ReadBytes(Marshal.SizeOf(myStructAr ray));

I didn't expect either to compile, and I was not disappointed.

------------------------------------

Later on I wanted to write the structure array back out to a binary file.
My first inclination was code similar to:

myBinaryWriter.Write(myStructArray);

but I did not actually expect it to compile since the Write method doesn't
have an overload for a generic "object" type.

Very soon I realized that I had no clue how to do it in C# and was
obviously
badly missing the point somewhere. Could someone please give me a good
swift
kick and enlighten me? I checked around the Web but only found simple
examples of reading/writing the primitive data types as binary, which was
no
help.

Thanks,
Ray Mitchell
Aug 10 '08 #3
Thanks - Just what I was looking for!

"Family Tree Mike" wrote:
BinaryFormatter should work fine.

FileStream fs = new FileStream("DataFile.dat", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, somestruct);

"Ray Mitchell" <Ra*****************@MeanOldTeacher.comwrote in message
news:4F**********************************@microsof t.com...
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));

Although using sizeof this way in C/C++ works fine, it results in a
compiler
error in C#. After inspecting the error further it suggested using the
following, which then compiled fine:

myBinaryReader.ReadBytes(Marshal.SizeOf(myStructAr ray));

But then I had the problem of where to store the bytes read since the
ReadBytes method returns a byte array but I wanted to store them into my
structure array. I tried both

myStructArray= myBinaryReader.ReadBytes(Marshal.SizeOf(myStructAr ray));
and
(byte[])myStructArray=
myBinaryReader.ReadBytes(Marshal.SizeOf(myStructAr ray));

I didn't expect either to compile, and I was not disappointed.

------------------------------------

Later on I wanted to write the structure array back out to a binary file.
My first inclination was code similar to:

myBinaryWriter.Write(myStructArray);

but I did not actually expect it to compile since the Write method doesn't
have an overload for a generic "object" type.

Very soon I realized that I had no clue how to do it in C# and was
obviously
badly missing the point somewhere. Could someone please give me a good
swift
kick and enlighten me? I checked around the Web but only found simple
examples of reading/writing the primitive data types as binary, which was
no
help.

Thanks,
Ray Mitchell

Aug 10 '08 #4

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

Similar topics

6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
4
by: Simon | last post by:
Hi all, I have a process, where I take a dataset from an SQL call, and need to write an XML file from that dataset. The data set can contain 10's of tables, each with 100's of rows, and I have...
2
by: Jeevan | last post by:
Hi, I have an array of data (which I am getting from a socket connection). I am working on a program which acts on this data but the program is written to work on data from a file (not from an...
3
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,...
2
by: phyzics | last post by:
I am porting an application from C++ to C#, and am having trouble finding a way to quickly and efficiently write structures to a binary file. In C++ this is trivial because all that is necessary is...
3
by: UsenetUser | 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...
7
by: John Dann | last post by:
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...
3
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...
6
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;
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.