Abundance FIGURE 5

{ Figure 5 Abundance }
Program Depend;
{ How Pascal uses explicit seeks/reads/writes to handle files. }
{ This program stores the Names and Birthdates of two }
{ dependent children in a standard DOS file allowed to grow }
{ big enough to hold up to 15 dependents. }
{ Then it prints them out. }
Const MaxDependents = 15;
   { the maximum number of dependents we could ever possibly have }
Type
  Dependent =
        Record
        ChildsName : String[30];
        ChildsBirthDate : Packed Array [1 .. 8] OF Char
          { stored MM/DD/YY as character }
          { should be between 1950 Jan 01 and Today }
          { this program does not enforce this }
        End; { Record }
Var
HighWaterDependents : 0 .. MaxDependents;
   { how many dependents we currently have }
DependentNumber : 1 .. MaxDependents;
   { index of the dependent we are looking at now }
ADependent : Dependent;
   { working storage record for current dependent }
Dependents : File of Dependent;
Procedure MaintainHighWater;
   Begin
   { corral a bad subscript back safely in bounds }
   If DependentNumber < 1
      Then DependentNumber := 1;
   If DependentNumber > MaxDependents
      Then DependentNumber := MaxDependents;
   { Keep track of the largest subscript used so far }
   If DependentNumber > HighWaterDependents
      Then HighWaterDependents := DependentNumber
   End; { MaintainHighWater }
Procedure ReadRecord;
   { reads record indexed by DependentNumber into ADependent }
   Begin
      MaintainHighWater;
      If DependentNumber > FileSize (Dependents)
         Then  { Record does not yet exist - fake it }
            Begin
            ADependent.ChildsName := ' ';
            ADependent.ChildsBirthDate := '        '
            End
         Else  { Record already exists }
            Begin
            Seek (Dependents, DependentNumber-1);
            Read (Dependents, ADependent)
            End
   End; { ReadRecord }
Procedure WriteRecord;
   { writes record indexed by DependentNumber from ADependent }
   Begin
      MaintainHighWater;
      Seek (Dependents,DependentNumber-1);
      Write (Dependents,ADependent)
   End; { WriteRecord }
Procedure SetupDependents;
   Begin
      DependentNumber := 1;
         ReadRecord;
         ADependent.ChildsName := 'Bruce';
         ADependent.ChildsBirthDate := '03/26/54';
         WriteRecord;
      DependentNumber  := 2;
         ReadRecord;
         ADependent.ChildsName := 'Brock';
         ADependent.ChildsBirthDate := '18/07/61';
         WriteRecord
   End { SetupDependents };
Procedure PrintDependents;
   Begin
   For DependentNumber := 1 To HighWaterDependents DO
       Begin
           ReadRecord;
           Writeln (Lst,DependentNumber:2, ' ',
              ADependent.ChildsBirthDate, '  ',
              ADependent.ChildsName)
       End; { For }
   Write(Lst, Char(12)) { eject the paper - works on most printers }
   End; { PrintDependents }
  Begin { Depend }
     HighWaterDependents := 0;
     Assign (Dependents,'C:Dep.Dat');
     Reset (Dependents);
     SetupDependents;
     PrintDependents;
     Close (Dependents)
  End. { Depend }
HTML Checked!
Canadian Mind Products You can get an updated copy of this page from http://mindprod.com/fig5.html