Handling Files in Delphi - Binary Files
(Page 3 of 4 )
Delphi also provides us with tools to read and write typed and untyped binary files.
Typed Files
Typed binary files are files that have a data type as the basic unit of writing and reading. You write a record to a file, and read the same unit of data back. Records are particularly useful, allowing us to store any mix of data types in the one file unit of data. Some people use records as a database to store their data. Although not very secure, it is very suitable for that purpose.
The syntax of a record is like so:
type
Trecordname = Record
fieldList1: type1;
...
fieldListn: typen;
end;
For example:
type
Tpeople = Record
name : string[20];
age : Integer;
work:string;
end;
In the example above we have created a record of type Tpeople, with its members as name, age and work.
You will notice that name is declared with string[20]. The - '[20]' - allocates twenty characters to this member. In other words, any name that you give has to be limited to twenty characters. If you don't give a number the string will be allocated space for 255 characters.
To illustrate, we will create a record called people and then demonstrate how to read and write to it.
Create a new application and add the following components: three Tedits, one Memo, and two Buttons, one captioned "Read from Record" and the other captioned "Write to Record." Then add the following type above the form type definition:
type
Tpeople = Record
name : string[20];
age : Integer;
work:string;
end;
It is a very simple record that takes three values, the name of the person, the age and the work they do. To actually use the record, we need to create a record variable like so:
variablename: recordtype;
so in the form variable section add the following code:
person:Tpeople;
We are going to use the person variable to access the record data.
Double click on the "write to record" button and add the following code:
procedure TForm1.Button1Click(Sender: TObject);
var
f:file of Tpeople;
begin
AssignFile(F, 'person.per');
ReWrite(F);
person.name:=edit1.Text;
person.age:=strtoint(edit2.Text);
person.work:=edit3.Text;
Write(f,person);
// Close the file
CloseFile(f);
end;
This procedure is writing the record data to a file called "person.per". Before we can write record data to the disk we need to declare a file type variable. This variable is declared like so:
Var variablename: file of Record;
In our code we declared it as f: file of Tpeople;
The next step is to assign the file variable to a file on a disk. The code:
AssignFile(F, 'person.per');
does the job for us. Once all the file connections have been set, the record is filled with data from the Tedits:
person.name:=edit1.Text;
person.age:=strtoint(edit2.Text);
person.work:=edit3.Text;
To access the fields of the record we use the record variable -person - with the dot notation. With the new record values safely stored in the person variable, the record data is written to the file like so:
Write(f,person);
And then file is closed with the closefile() function.
Double click on the "Read from Record" button and add the following code:
procedure TForm1.Button2Click(Sender: TObject);
var
F : File of tpeople; // A file of personrecords
begin
AssignFile(F, 'person.per');
Reset(f);
// Display the file contents
while not Eof(f) do
begin
Read(f, person);
memo1.lines.Add(person.name+#13#10+inttostr(person.age)
+#13#10+person.work);
end;
// Close the file for the last time
CloseFile(f);
end;
After assigning the file variable, the code runs a while loop and reads all of the record entries from the file:
while not Eof(f) do
begin
Read(f, person);
emo1.lines.Add(person.name+#13#10+inttostr(person.age)+#13#10+person.work);
end;
The Eof (f) stands for end of file, and tells the while loop to read until it reaches the end of the file. All the contents are then added to the memo.
As you can see, using typed binary files does have advantages. You can store as many records as you like and even go to specific entries in a record. This almost gives it a database-like functionality. Delphi provides us with a couple of functions to move around to different positions within a file:
Seek() function enables us to go to specific locations in a record. The syntax is:
Seek(file variable, position);
For example, to go to the fourth place in a record:
Seek(F, 5);
Or to go to the end after the last record:
Seek(F, FileSize(F));
Next: Streams >>
More Delphi-Kylix Articles
More By Jacques Noah