471,311 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,311 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 10892
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Sebastian Kemi | last post: by
3 posts views Thread by Matt Laver | last post: by
2 posts views Thread by phyzics | last post: by
3 posts views Thread by Usenet User | last post: by
7 posts views Thread by John Dann | last post: by
6 posts views Thread by arne.muller | last post: by

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.