473,394 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 11110
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: 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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.