home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C!T ROM 2
/
ctrom_ii_b.zip
/
ctrom_ii_b
/
PROGRAM
/
PASCAL
/
PASTUT34
/
RECORDS.TXT
< prev
next >
Wrap
Text File
|
1993-01-20
|
5KB
|
144 lines
RECORDS.
▀▀▀▀▀▀▀▀
A record type has a set number of components or fields, all of which
can be of different types. The record type declaration specifies the
identifier that names the field and the type of each field. The syntax
is illustrated in the extract from the program RECORDS.PAS shown below:
Type
SaleType = Record { A record of sales containing }
Name : string[50]; { two string fields and three }
Item : string[20]; { numeric fields. }
Quantity : integer;
UnitPrice : real;
VAT : real;
end;
In the VAR declaration part of the program, specific instances of a
record type are declared, as illustrated below:
Var
Sale : SaleType; {An instance of the SaleType record.}
Such record-type variables can then be used like any other variable in the
program, with access to an individual field by either of two methods:
1. by entering the record variable name followed by a period and then
the individual field name, as illustrated below:
Until .... (Sale.Name <> '') and (Sale.Name[0] < #50);
2. by use of the WITH statement, described in the notes STRUCT.TXT
and illustrated below:
procedure ReadRecord(Filename : string; Recpos : longint);
begin
....
With Sale do
begin
....
GoToXY(28,3);
ClrEol;
write(Name);
....
end;
end; {Proc ReadRecord}
Record variables can be either global or local and may contain fields
that are themselves records.
Records can contain a variant part, such that the field structure may
be varied according to the resolution of a CASE statement ( see pages
31-2 of the Programmer's Guide).
Since records are usually associated with a database and the data is
stored in a file on disk, the record-type variable is frequently
assigned to a file by a declaration such as:
SalesFile : File of SaleType; { A file of SaleType records.}
Such files of records can be read from, or written to, by random access,
using the Seek procedure (see below and page 122 of the Library Reference).
By comparison, text files must be accessed sequentially.
File manipulation involves the procedures Assign, Rewrite, Reset, Read,
Write and Close, which are described in the note FILES.TXT and illustrated
by the procedures and statements used in the program RECORDS.PAS as below:
procedure CreateFile(Filename : string); { To create a new file on disk, }
begin { open it by rewriting it, so }
Assign(SalesFile,Filename); { that there are no records }
Rewrite(SalesFile); { preserved from a possible }
Open := True; { previous file with same name. }
end; {Proc CreateFile}
procedure OpenFile(Filename : string); { To open an existing file on }
begin { disk and reset it, so that }
Assign(SalesFile,Filename); { old records are preserved. }
Reset(SalesFile);
Open := True;
end; {Proc OpenFile}
procedure ReadRecord(Filename : string; Recpos : longint);
begin
....
Seek(SalesFile,RecPos);
Read(SalesFile,Sale);
....
end; {Proc ReadRecord}
procedure AppendRecord(Filename : string); { To append new data to the}
begin { currently open disk file.}
Seek(SalesFile,FileSize(SalesFile));
Write(SalesFile,Sale);
....
end; {Proc AppendRecord}
{Main program}
begin
....
....If Open = True then Close(SalesFile);
....
end.
The procedure Seek moves the file pointer to the start of the record
specified in the second parameter. The first parameter being the file
variable name, as illustrated above and again below:
procedure ChangeRecord(Filename : string; Recpos : longint);
begin
....
Seek(SalesFile,Recpos);
write(SalesFile,Sale);
....
end;
The program RECORDS.PAS is some 400 lines long because a number of
procedures and statements are used to display information on the
screen in windows and to access the disk to check the names of
existing record files with extension names .REC, etc. However, the
statements and procedures relating to records are adequately
described above and should enable the reader to create a simple
record program, especially if some of the procedures used in
the present program RECORDS.PAS are 'copied' and 'pasted' to the
new program using the ClipBoard. Transfer via the ClipBoard is
achieved by highlighting the required code, using Ctrl-B at the
beginning and Ctrl-K at the end and then using the Edit Menu and
the Copy and Paste sub-menu options.
Since a database of records will occupy a large amount of memory and
there is need for the database to grow as required, dynamic memory
allocation using the Heap is usually necessary (see the notes on the
Heap and Pointers entitled HEAP&PTR.TXT).
RECORDS.TXT
20.1.93