Understanding what files are and choosing a Delphi file type - part 3

What is a File?  How are they stored? What format is best for my project? - The final part of a series by Philip Rayment


The final example uses a simple text file for storage:

Example 3: Text File
procedure WriteFile(filename:string);
var
  fil: text;
  i: integer;
begin   {WriteFile}
  AssignFile(fil,filename); rewrite(fil);{Create the file}
  {Write the file version and number of records}
  Writeln(fil,LatestFileVersion,' ',length(People));[6]
  for i:=0 to high(people) do
    with people[i] do begin	{Write the data}
      Writeln(fil,ChristianName);			[6,10]
      Writeln(fil,Surname);				[7,14]
      Writeln(fil,Address1);				[21,9]
      Writeln(fil,Address2);				[2,24]
      Writeln(fil,Town);					[13,19]
      Writeln(fil,Postcode,' ',YearsService,' ',ID,' ',DateToStr(Birthdate)); [24,23]
    end;   {with}
  CloseFile(fil);
end;   {WriteFile}

procedure ReadFile(filename:string);
var
  ver: byte;
  i, num: integer;
  d: string;
begin   {ReadFile}
  AssignFile(fil,filename); reset(fil);		  {Open the file}
  readln(fil,ver,num);	{Read the version number and number of records}
  SetLength(people,num);
  for i:=0 to pred(num) do
      with people[i] do begin			  {Read the data}
        Readln(fil,ChristianName);
        Readln(fil,Surname);
        Readln(fil,Address1);
        Readln(fil,Address2);
        Readln(fil,Town);
        Readln(fil,Postcode,YearsService,ID,d);
        Birthdate:=StrToDate(d);
      end;   {with}
  CloseFile(fil);
end;   {ReadFile}

Analysis

The total file size is 178 bytes, not much more than the untyped file. Delphi automatically converts the numbers to text, but we have to use the DateToStr function to convert the date to text.

This table provides comparative statistics on the three examples:

 

Untyped files

Typed Files

Text Files

File size

143 bytes

342 bytes

178 bytes

Bytes in "header"

3

114

6

Average bytes per record

70

114

86

Lines of code in WriteFile()

28

12

17

Estimate of speed

Probably fairly fast, as no conversions were required, but there were many different calls to BlockWrite and several calls to a subroutine.

Probably fastest, as just three separate writes to the disk were involved.

Probably the slowest, due to all the conversions to text required.

The Advantages and disadvantages of the various file types are as follows:

Untyped Files

Advantages

  • You can store anything you wish in Untyped Files. There are no restrictions.
  • Untyped Files will normally be the most compact

Disadvantages

  • You have to keep track of the data yourself, which will normally require a fair bit of coding.
Typed Files

Advantages

  • Typed Files are easier to use than Untyped files.
  • Typed Files are probably the fastest for most purposes as the data can be loaded straight into the record. The other types usually involve more conversions and/or data shuffling.

Disadvantages

  • You are limited to one type of data per file.
  • Records have to be designed to hold the largest data (e.g. the longest possible name) and all records thus take up this space, so a Typed File is generally the most space hungry.
  • It is pointless writing pointers to the file (if you did, you would simply write and read the memory address, not the data itself), so you cannot have file of pointers, objects, or strings (longstrings) or records containing any of these. In any case the compiler will not allow a file of string (longstring).
Text Files

Advantages

  • Delphi has special facilities for handling Text Files, such as conversion of numerical data to text and vice versa, making Text Files easy to use and fairly compact.
  • Text Files can be viewed in a text editor or even dumped to the screen (or printer) at the command prompt with the TYPE command.
  • Corrupted files can be edited with a text editor.

Disadvantages

  • Non-textual data (other than numbers) cannot be included, unless somehow converted into a textual form.
  • Not efficient storage of non-textual data.

 


Appendix A

The following table compares how to code selected actions for various file types.

 

Untyped files

Typed Files

Text Files

Declaring

var f: file;
    v: type;
    i:  integer;
var f: file of type;
    r: type;
    i:  integer;
var f:  TextFile;
    i:  integer;
    s:  string;
    ch: char;

Assigning

AssignFile(f, filename)

AssignFile (f, filename)

AssignFile(f, filename);

Opening for reading

FileMode:=0;

Reset(f, 1);

FileMode:=0;

Reset(f);

Reset(f);

Opening for reading and writing

Reset(f, 1);

Reset(f);

Not available

Opening for appending

Reset(f, 1);

Seek(f, filesize(f));

Reset(f);

Seek(f, filesize(f));

Append(f);

Creating

Rewrite(f, 1);

Rewrite(f);

Rewrite(f);

Reading

BlockRead(f, v, sizeof(v));

Read(f, r);

Read(f, i);

Read(f, s);

Readln(f, i, ch, s);(1)

Skip a record/line while reading

Seek(f, filepos(f)+sizeof(v));

Seek(f, succ(filepos(f)));

Readln(f); (2)

Writing

BlockWrite(f, v, sizeof(v));

Write(f, r);

Write(f, i);

Write(f, s);

Writeln(f, i, ‘ ‘, s); (1)

Get the current file position

i:=filepos(f);

i:=filepos(f);

Not available

Jump to a position in the file

Seek(f, i);

Seek(f, i);

Not available

Get the file size

i:=filesize(f);

i:=filesize(f);

Not available (3)

Closing

CloseFile(f);

CloseFile(f);

CloseFile(f);

  1. The Read, ReadLn, Write, and WriteLn procedures can take multiple arguments.
  2. If the ReadLn procedure has no parameters (other than the file variable), the file pointer merely moves to the end of the line. If the WriteLn procedure has no parameters (other than the file variable), a blank line is output.
  3. See the Tip How do I get the size of a Text File in Delphi?

<< Part 1 << Part 2 Part 3