1. Technology

Create your own Database using Delphi's "File Of" Typed Files

Understanding Typed Files


Scenario: In my Delphi program I need a way of storing some information to the disk. I do not want to work with text files - there should be no easy way to see/alter the information in a file. There may be actions like changing/updating the existing data; but mainly writing and reading will take place. All the information is stored in my user-defined Delphi record data type. What approach should I use?

BDE or ADO plus Paradox or Access, ... well no thanks ... I do not want to bother with the BDE. Should I use the ASCII text files approach, hm no, there should be some minimal protection - text files are "to visible". Delphi has the answer! The answer is: typed files (or files of some type/binary files).


Simply put a file is a binary sequence of some type. In Delphi there a three classes of file: typed, text, and untyped. Typed files are files that contain data of a particular type, such as Double, Integer or previously defined custom Record type. Text files contain readable ASCII characters. Untyped files are used when we want to impose the least possible structure on a file.

Typed Files

While text files consist of lines terminated with a CR/LF (#13#10)combination, typed files consist of data taken from a particular type of data structure.

For example, the following declaration creates a record type called TMember and an array of TMember record variables.

   TMember = record
     Name : string[50];
     eMail : string[30];
     Posts : LongInt;
  var Members : array[1..50] of TMember; 
Before we can write the information to the disk we have to declare a variable of a file type. The following line of code declares an F file variable.
 var F : file of TMember; 
Note: To create a typed file in Delphi, we use the following syntax:

var SomeTypedFile : file of SomeType

The base type (SomeType) for a file can be a scalar type (like Double), an array type or record type. It should not be long string, dynamic array, class, object or a pointer.

In order to start working with files from Delphi we have to link a file on a disk to a file variable in our program. To create this link we must use AssignFile procedure in order to associate a file on a disk with a file variable.

 AssignFile(F, 'Members.dat') 
Once the association with an external file is established, the file variable F must be 'opened' to prepare it for reading and/or writing. We call Reset procedure to open an existing file or Rewrite to create a new file. When a program completes processing a file, the file must be closed using the CloseFile procedure. After a file is closed, its associated external file is updated. The file variable can then be associated with another external file.

In general, we should always use exception handling; many errors may arise when working with files. For example: if we call CloseFile for a file that is already closed Delphi reports an I/O error. On the other hand, if we try to close a file but have not yet called AssignFile, the results are unpredictable.

Write to a File

Suppose we have filled an array of Delphi members with their names, e-mails and number of posts and we want to store this information in a file on the disk. The following peace of code will do the work:
   F : file of TMember;
   i : integer;
  AssignFile(F,'members.dat') ;
  Rewrite(F) ;
   for j:= 1 to 50 do
    Write (F, Members[j]) ;
   CloseFile(F) ;

Read from a File

In order to retreive all the information from the 'members.dat' file we could use this code:
   Member: TMember
   F : file of TMember;
  AssignFile(F,'members.dat') ;
  Reset(F) ;
   while not Eof(F) do begin
    Read (F, Member) ;
   CloseFile(F) ;
Note: Eof is the EndOfFile checking function. We use this function to make sure that we are not trying to read beyond the end of the file (beyond the last stored record).

Seeking and Positioning

Files are normally accessed sequentially. When a file is read using the standard procedure Read or written using the standard procedure Write, the current file position moves to the next numerically ordered file component (next record). Typed files files can also be accessed randomly through the standard procedure Seek, which moves the current file position to a specified component. The FilePos and FileSize functions can be used to determine the current file position and the current file size.
 {go back to the beginning - the first record}
 Seek(F, 0) ;
 {go to the 5-th record}
 Seek(F, 5) ;
 {Jump to the end - "after" the last record}
 Seek(F, FileSize(F)) ; 

Change and Update

We just learned how to write and read the entire array of Members. What when all we want to do is to seek to the 10-th member and change his e-mail? The next procedure does exactly that:
 procedure ChangeEMail(const RecN : integer; const NewEMail : string) ;
 var DummyMember : TMember;
  {assign, open, exception handling block}
  Seek(F, RecN) ;
  Read(F, DummyMember) ;
  DummyMember.Email := NewEMail;
  {read moves to the next record, we have to
  go back to the original record, then write}
  Seek(F, RecN) ;
  Write(F, DummyMember) ;
  {close file}

All done!

That's it, now we have all we need to accomplish our task. We can write Members information to the disk, we can read it back and we can even change some of the data (e-mail, for example) in the "middle" of the file.

What's important is that this file is not an ASCII file, this is how it looks in Notepad (only one record):

 .Delphi Guide g Ò5·¿ì. 5. . B V.Lƒ ,„¨.delphi@aboutguide.comÏ.. ç.ç.ï.. 
Next time we'll see how to use untyped files in Delphi - those that impose the least possible structure on a file, or have no structure whatsoever. Untyped files are most commonly used when doing direct access to disk files, that is, operations like copying.
Related Video
Export Data From an Excel Sheet to a Word Document

©2014 About.com. All rights reserved.