473,387 Members | 3,750 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,387 software developers and data experts.

Read a file of records with a Struct in C#

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

Oct 5 '06 #1
3 2146
Hello, Eddieb7!

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

EHI,

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

Ei.e
E FileName = File of TempRecType

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

ETo read from this file I would simply create a variable of
ETempRecType
Eand read the
Efile straight into the variable.

Ecan 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/lib...erializer.aspx )
and here
(
http://msdn2.microsoft.com/en-us/lib...formatter.aspx )

--
With best regards, Vadym Stetsyak.
Blog: http://vadmyst.blogspot.com
Oct 5 '06 #2
Eddieb7 wrote:
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.Bin aryFormatter
on top of a stream.
Arne
Oct 6 '06 #3
Thanks Guys,

Both your comments have been very helpful!!

Oct 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Van Altena via .NET 247 | last post by:
I'm trying to figure out how to read a formatted binary file intoa structure definition in C#. I've tried using the"StructLayout" attribute with both LayoutKind.Explicit andLayoutKind.Sequential...
6
by: PengYu.UT | last post by:
Hi, Suppose I a file with the following format --- each line is either a record or a blank line. If it is a record, it might be of one of the following format, where the first format means t...
5
by: Vasilis Serghi | last post by:
Hi all, I am fairly new to C programming and I have come to a stand still. I am trying to create a program that will accept a user input and give the user an output depending on the contents of a...
3
by: happy | last post by:
/* Book name : The prodessional programmers guide to C File name : E:\programs\tc\iti01\ch09\main\01setupm.c Program discription: file setuping -up -Version 01-ver01-W Logic ...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
0
by: magicbeans | last post by:
I am trying to create a .bin file of records from a .txt file. Initially the records are stored in a text file and I want to Read from the .txt file and Write to the .bin file. This is the C++...
3
by: computerwolf8 | last post by:
I have a file where I know the lines go as follows: string long string int int string double
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.