home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue149 / delphi / copydelp.exe / RecFile / rec1.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-03-03  |  5.8 KB  |  208 lines

  1. unit Rec1;
  2. { PC Plus - sample Delphi program.
  3.   Illustrates reading and writing records to a typed file.
  4.   Note, little error-checking is done, so don't try anything clever! } 
  5.  
  6. interface
  7.  
  8. uses
  9.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  10.   Forms, Dialogs, StdCtrls;
  11.  
  12. type
  13. { For compatibility between Delphi 1 and Delphi 2   }
  14. { strings are fixed size (you can't write Delphi 2  }
  15. { dynamic strings to a typed file) and the smallint }
  16. { data type is used (the size of integer varies     }
  17. { between Delphi 1 and 2)                           }
  18. ThingRec = record
  19.   name        : string[20];
  20.   description : string [50];
  21.   value       : smallint;
  22. end;
  23.  
  24.  
  25.  
  26.  
  27.   TForm1 = class(TForm)
  28.     CountBtn: TButton;
  29.     Edit1: TEdit;
  30.     Edit2: TEdit;
  31.     Edit3: TEdit;
  32.     Label1: TLabel;
  33.     Label2: TLabel;
  34.     Label3: TLabel;
  35.     Label4: TLabel;
  36.     FilePosLabel: TLabel;
  37.     RecNumEd: TEdit;
  38.     LoadBtn: TButton;
  39.     SaveBtn: TButton;
  40.     procedure CountBtnClick(Sender: TObject);
  41.     procedure LoadBtnClick(Sender: TObject);
  42.     procedure SaveBtnClick(Sender: TObject);
  43.     procedure FormActivate(Sender: TObject);
  44.   private
  45.     { Private declarations }
  46.   public
  47.     { Public declarations }
  48.     procedure DisplayRecord( aRecord: ThingRec );
  49.     function FieldsOK : boolean;
  50.   end;
  51.  
  52.  
  53.  
  54. const
  55.   RecFileName = 'Test.dat';
  56.  
  57. var
  58.   Form1: TForm1;
  59.   RecFile          : File of ThingRec;
  60.   RecPos           : integer;
  61.  
  62. implementation
  63.  
  64. {$R *.DFM}
  65. function TForm1.FieldsOK : boolean;
  66. { Check that the data in the Edit boxes is valid for a ThingRec record }
  67. { Return true if it is. False otherwise.                               }
  68. { Call this function before attempting to save a record to disk        }
  69. var
  70.   i   : integer;
  71.   msg : string;
  72. begin
  73.   msg := '';
  74.   { do all 3 edit boxes contain data? }
  75.   if (Edit1.Text = '') or
  76.      (Edit2.Text = '') or
  77.      (Edit3.Text = '') then
  78.         msg := 'You must add data to each of the fields!'
  79.   else
  80.   try   { check that ValEd.Text can be converted to an integer }
  81.     i := StrToInt(Edit3.Text);
  82.     except
  83.       on EConvertError do
  84.       begin
  85.          i := 0;
  86.          msg := 'You must enter an integer into the Value field!';
  87.       end;
  88.   end; { try,except }
  89.   { If no error msg was initialised, the data is OK, return true }
  90.   { Otherwise, there was a problem, return false                 }
  91.   if msg <> '' then
  92.   begin
  93.     MessageDlg(  msg, mtInformation, [mbOk], 0);
  94.     result := false;
  95.   end
  96.   else
  97.     result := true;
  98. end;
  99.  
  100. procedure TForm1.DisplayRecord( aRecord: Thingrec );
  101. { Display the fields of record, aRecord in the Edit boxes on the form }
  102. begin
  103.   With aRecord do
  104.   begin
  105.     Edit1.Text := name;
  106.     Edit2.Text := description;
  107.     Edit3.Text := IntToStr(value);
  108.   end;
  109.   FilePosLabel.Caption := IntToStr( RecPos );
  110. end;
  111.  
  112. procedure TForm1.CountBtnClick(Sender: TObject);
  113. { Count the records in the data file }
  114. var
  115.    size             : longint;
  116. begin
  117.     size := 0;
  118.     if not FileExists( RecFileName ) then       { Check that input file exists }
  119.         ShowMessage('File: ' + RecFileName + ' not found!')
  120.     else
  121.     begin
  122.       AssignFile(RecFile, RecFileName );
  123.       Reset(RecFile);
  124.       size := FileSize(RecFile);
  125.       CloseFile(RecFile);
  126.       ShowMessage('File: ' + RecFileName + ' contains ' + IntToStr(size)
  127.                                                         + ' records')
  128.     end;
  129. end;
  130.  
  131. procedure TForm1.LoadBtnClick(Sender: TObject);
  132. var
  133.   RecNum        : integer;
  134.   invalidNumber : boolean;
  135.   aRecord       : ThingRec;
  136. begin
  137.    invalidNumber := false;
  138.    try   { check that ValEd.Text can be converted to an integer }
  139.       RecNum := StrToInt(RecNumEd.Text);
  140.     except
  141.       on EConvertError do
  142.       begin
  143.          RecNum := 0;
  144.          MessageDlg(  '"' + RecNumEd.Text + '" is not a valid integer!',
  145.                       mtInformation, [mbOk], 0);
  146.          invalidNumber := true;
  147.       end
  148.     end; { except }
  149.     if not invalidNumber then
  150.     begin
  151.          if not FileExists( RecFileName ) then
  152.             ShowMessage('File: ' + RecFileName + ' not found!')
  153.          else
  154.          begin
  155. { ----- Open the file ----- }
  156.            AssignFile(RecFile, RecFileName );
  157.            Reset(RecFile);
  158.            { make sure the specified record number is valid }
  159.            if (RecNum < 0) or (RecNum > (FileSize(RecFile)-1)) then
  160.               ShowMessage('Record Number ' + IntToStr(RecNum) + ' not found!')
  161.            else
  162.            begin
  163. { ----- Seek and read the record at the specified position -----     }
  164.              Seek( RecFile, RecNum );
  165.                    { update the global var, RecPos                   }
  166.              RecPos := FilePos( RecFile );
  167.              Read( RecFile, aRecord );
  168.                    { calll procedure to display fields in Edit boxes }
  169.              Displayrecord( aRecord );
  170.            end;
  171.            CloseFile(RecFile);
  172.          end;
  173.     end;
  174.  
  175. end;
  176.  
  177. procedure TForm1.SaveBtnClick(Sender: TObject);
  178. var
  179.   aRecord : ThingRec;
  180. begin
  181.   if FieldsOK then
  182.   if not FileExists( RecFileName ) then
  183.      ShowMessage('File: ' + RecFileName + ' not found!')
  184.   else
  185.   begin
  186.      { assign values to the record fields }
  187.      aRecord.name := Edit1.Text;
  188.      aRecord.description := Edit2.Text;
  189.      aRecord.value := StrToInt(Edit3.Text);
  190.      { then open file, seek the RecPos (the position of the record that }
  191.      { was last loaded, then write the new record into the file at      }
  192.      { this position                                                    }
  193.      AssignFile(RecFile, RecFileName);
  194.        { Don't Rewrite the file or you'll destroy all existing data }
  195.      Reset(RecFile);
  196.      Seek(RecFile, RecPos );
  197.      Write(RecFile, aRecord);
  198.      CloseFile(RecFile);
  199.   end;
  200. end;
  201.  
  202. procedure TForm1.FormActivate(Sender: TObject);
  203. begin
  204.   RecPos := 0;
  205. end;
  206.  
  207. end.
  208.