Tuesday, November 3, 2009

11. TYPE DATA RECORD files in Pascal

A group of variables can be put together to form a record and that record can be described to pascal as a type of occurrence just like integer or string.
For example a description of members of an organisation could be handled as a type. Each member has a name, a city he came from, the state where that city located and the zip code of the city. This group of information describes a member of that group. This is a member record. This can be described as a type.

Type
memberec = Record
name,city,state : string;
zip : string;
End;
Once you defined it to pascal, we can use that record type in the program by assigning a variable to it. just like we declare variables of type integer. We also declare memberfile consists of all these member records.

var
memberfile : file of memberec;
filerec, seekrec :memberec;

Now we can use it just like any type . Since it consists of a group of variables, we have to specify which variable we want by dotting.
ex: filerec.name
etc.

The following a program which generates a number of member records of a type and reads them afterwords and displays them. The file is a random data file and assigned a name and opened for reading or writing. The record numbering starts with zero and continues. We use seek and record number to access a particular record. and read it to get the data.
seek(memberfile,indx);
read(memberfile,seekrec);

Program program11;
(* Author: Rao S Lakkaraju *)
(* Record Types and Data Files *)
uses crt,graph;

Type
memberec = Record
name,city,state : string;
zip : string;
End;
var
memberfile : file of memberec;
filerec, seekrec :memberec;

indx : integer;
sindx : string;

Procedure displayrec(filerec : memberec);
begin
write(filerec.name);
write(filerec.city);
write(filerec.state);
writeln(filerec.zip);
end;

Procedure writemember( filerec : memberec);

begin

for indx := 0 to 5 do
begin
str(indx,sindx);
filerec.name := sindx + 'rao';
filerec.city := sindx + 'aurora';
filerec.state:= sindx + 'IL';
filerec.zip := sindx + '60504';
displayrec(filerec);
write(memberfile,filerec);
end;

end;


Begin
clrscr;
textBackground(white);
Assign(memberfile, 'c:\member.dat');
ReWrite(memberfile);
Textcolor(Red);
writeln('Generated File Records');
writemember(filerec);
textcolor(green);
writeln('Seek file records');
indx:= 0;

repeat
seek(memberfile,indx);
read(memberfile,seekrec);
displayrec(seekrec);
indx := indx + 1;
until eof(memberfile);


writeln('My eleventh Pascal Program');
readln;
End.

Summary: A logical group of pascal variables could be described as a type and that type could be used in a pascal program by assigning a variable to it. The records of this type could be stored in a data file and accessed randomly for reading and writing.

No comments:

Post a Comment