Read a file of records with a Struct in C#

  • Thread starter Thread starter Eddieb7
  • Start date Start date
E

Eddieb7

HI,

I come from a dephi background and i am in the process of moving to c#.
In Delphi I can declare a file as being of a recordrtpe (or Struct in
c#).

i.e
FileName = File of TempRecType

where TempRecTypeis declared as
Record TempRecType
longint TempIntVal,
string[14] Shortstring,
shortint TempIntVal2
end;

To read from this file I would simply create a variable of TempRecType
and read the
file straight into the variable.

can anybody help me to do this in C#?

Cheers
Eddie
 
Hello, Eddieb7!

You wrote on 5 Oct 2006 13:54:53 -0700:

E> HI,

E> I come from a dephi background and i am in the process of moving to
E> c#.
E> In Delphi I can declare a file as being of a recordrtpe (or Struct in
E> c#).

E> i.e
E> FileName = File of TempRecType

E> where TempRecTypeis declared as
E> Record TempRecType
E> longint TempIntVal,
E> string[14] Shortstring,
E> shortint TempIntVal2
E> end;

E> To read from this file I would simply create a variable of
E> TempRecType
E> and read the
E> file straight into the variable.

E> can anybody help me to do this in C#?

In .NET world this is done through serialization.
Have a look at here
(
http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx )
and here
(
http://msdn2.microsoft.com/en-us/li...zation.formatters.binary.binaryformatter.aspx )
 
Eddieb7 said:
I come from a dephi background and i am in the process of moving to c#.
In Delphi I can declare a file as being of a recordrtpe (or Struct in
c#).

i.e
FileName = File of TempRecType

where TempRecTypeis declared as
Record TempRecType
longint TempIntVal,
string[14] Shortstring,
shortint TempIntVal2
end;

To read from this file I would simply create a variable of TempRecType
and read the
file straight into the variable.

can anybody help me to do this in C#?

Two different approaches:

1) read/write individual parts with System.IO.BinaryReader/BinaryWriter
on top of a stream

2) read/write using
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
on top of a stream.


Arne
 

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

Back
Top