473,785 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2164
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.Binar yReader/BinaryWriter
on top of a stream

2) read/write using
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er
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
4613
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 options. I can get this to worksuccessfully, but only when I'm NOT dealing with arrays in thestructure definition. For example: In C, you are able to simply read a binary file intoa structure as follows: ...
6
1339
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 equals 1 by default. w h w h t
5
16655
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 csv file. So far I have successfully managed to capture the user input, open the csv file, create a structure that matches the file data, but I cannot get the data into the structure to do a search on. This is some of what I have done so far:- ...
3
2996
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 : 1-open file for output 2-while more customers 2-1-input customers record 2-2-write customer record to file 3-write end of file record to file 4-close file
8
23907
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
5212
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 facing problems: Assume that FILE* filePointer; unsigned char lineBuffer;
0
1900
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++ code. The result write exactly but it has funny-looking unwanted characters attached at the end of the .bin file. How do I fix it? #include <iostream> #include <fstream> using namespace std; struct details { int id;
3
4963
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
3713
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 consists of structures of type---- struct record { int acountnum; char name; float value;
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9491
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.