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