Typed files are binary files that contain items of identical size. Typed files are usually files that contain records. To create a typed file, you first have to create a new file data type using the following syntax:
type NewFileType = file of DataType;
The following code illustrates how to create a new file type that can be used to read and write records to a typed file:
type TPerson = record FirstName: string[20]; LastName: string[30]; Age: Integer; end; TPersonFile = file of TPerson;
Notice that string fields in the record declaration have an explicitly defined length. The string length must be explicitly defined because the size of the entire record must be constant. If you want to store the record to a disk file, you cannot use normal strings since their length can change at any time and the compiler cannot determine their length at compile time.
There are several differences between text and typed files:
When you reset a typed file you can read and write to the file (you can only read from a text file when you reset it).
When you read or write from a typed file, you have to use the Read and Write procedures, not ReadLn and WriteLn.
The following example shows how to work with typed files.
Listing 8-6: Working with typed files
program Project1; {$APPTYPE CONSOLE} uses SysUtils; type TPerson = record FirstName: string[20]; LastName: string[30]; Age: Integer; end; TPersonFile = file of TPerson; procedure ReadRecord(const AFileName: string; var Rec: TPerson); var F: TPersonFile; begin AssignFile(F, AFileName); {$I-} Reset(F); {$I+} if IOResult = 0 then begin Read(F, Rec); CloseFile(F); end; // if IOResult end; procedure WriteRecord(const AFileName: string; var Rec: TPerson); var F: TPersonFile; begin AssignFile(F, AFileName); Rewrite(F); Write(F, Rec); CloseFile(F); end; procedure DisplayRecord(var Rec: TPerson); begin WriteLn('First Name: ', Rec.FirstName); WriteLn('Last Name: ', Rec.LastName); WriteLn('Age: ', Rec.Age); WriteLn; end; var TestRec: TPerson; ReadRec: TPerson; begin TestRec.FirstName := 'Stephen'; TestRec.LastName := 'King'; TestRec.Age := 58; WriteRecord('c:\info.dat', TestRec); ReadRecord('c:\info.dat', ReadRec); DisplayRecord(ReadRec); ReadLn; end.
Another major difference between text and typed files is that while text files only allow sequential access, typed files allow you to randomly access records in the file. Random access is possible because every record in the file is the same size, and to read a specific record, procedures only have to skip a certain, easily determinable, amount of bytes.
To determine the number of records in a file, you can use the FileSize function. To move to a specific record in the file, you can use the Seek procedure. The Seek procedure accepts two parameters: the file variable and a zero-based integer that specifies the number of the record to move to.
var F: TPersonFile; RecCount: Integer; begin RecCount := FileSize(F); if RecCount = 0 then WriteLn('File is empty') else Seek(F, FileSize(F)); end.