Address Book Program

Teaching Programming with Turbo Pascal

Copyright © by Mark Baker 1997

Copyright waiver

Return to MarkChrisSoft home page


This is the sheet that I give to pupils when they start to program the address book program.

Address Book - Program Specification

Saving and Loading Text Files

To save a text file, you must do these things:
  1. Declare a text file variable.
  2. Assign the text file variable to a real file name.
  3. Turn the I/O checking off.
  4. Try and open the file for writing to.
  5. Turn the I/O checking back on.
  6. Check to see if the file opened successfully.
  7. If it did, save your arrays and close the file, else output an error message.
To load a text file, you must do these things:
  1. Declare a text file variable.
  2. Assign the text file variable to a real file name.
  3. Turn the I/O checking off.
  4. Try and open the file for reading from.
  5. Turn the I/O checking back on.
  6. Check to see if the file opened successfully.
  7. If it did, load your arrays and close the file, else output an error message.
Outline procedures for saving and loading are given below.
{---------------------------------------------------------}
procedure Save(Name,Address,Phone: ArrayType);
{---------------------------------------------------------}
var
  i: integer;
  fdat : text;

    begin
    assign(fdat,'address.dat');	{Give the text variable a 
                                           real file name}
    {saving the file}			
    {$I-}                               {Open the file for writing to,
    rewrite(fdat);			with the input checking off}
    {$I+}
				
    if IOResult=0 then			{File opened OK}
      begin
	for i:=1 to maxNames do
	  begin
  	  writeln(fdat,Name[i]);    	{Writing to the text file}
	  writeln(fdat,Address[i]);
	  writeln(fdat,Phone[i]);
	  end;
      close(fdat);			{Opened files must be closed}
      end
        else Warning ('An error has occurred whilst saving! Is your disc full?');
     end;  {Save procedure}



{---------------------------------------------------------}
procedure Load(var Name,Address,Phone: ArrayType);
{---------------------------------------------------------}
var
  i: integer;
  fdat : text;

    begin
    assign(fdat,'address.dat');	{Give the text variable a 
                                           real file name}
    {loading the file}			
    {$I-}                                 {Open the file for reading,
    reset(fdat);                           with the input checking off}
    {$I+}					
				
    if IOResult=0 then                    {File opened OK}
      begin
	for i:=1 to maxNames do
	  begin
  	  readln(fdat,Name[i]);           {Writing to the text file}
	  readln(fdat,Address[i]);
	  readln(fdat,Phone[i]);
	  end;
      close(fdat);                        {Opened files must be closed}
      end
        else Warning ('File not found!');
     end;  {Save procedure}


Return to MarkChrisSoft home page

Author: Mark Baker, e-mail mbaker@rmplc.co.uk
Last revision: 4th January 1997