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

R

Ray Mitchell

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

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(myStructArray));
and
(byte[])myStructArray=
myBinaryReader.ReadBytes(Marshal.SizeOf(myStructArray));

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
 
J

Jon Skeet [C# MVP]

Ray Mitchell said:
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.
 
F

Family Tree Mike

BinaryFormatter should work fine.

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

Ray Mitchell

Thanks - Just what I was looking for!

Family Tree Mike said:
BinaryFormatter should work fine.

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

Ray Mitchell said:
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(myStructArray));

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(myStructArray));
and
(byte[])myStructArray=
myBinaryReader.ReadBytes(Marshal.SizeOf(myStructArray));

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
 

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