home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / PASCAL / PASTUT34 / RECORDS.TXT < prev    next >
Text File  |  1993-01-20  |  5KB  |  144 lines

  1.                                 RECORDS.
  2.                                 ▀▀▀▀▀▀▀▀
  3. A record type has a set number of components or fields, all of which
  4. can be of different types. The record type declaration specifies the
  5. identifier that names the field and the type of each field. The syntax
  6. is illustrated in the extract from the program RECORDS.PAS shown below:
  7.  
  8.   Type
  9.      SaleType = Record                  { A record of sales containing }
  10.        Name       : string[50];         { two string fields and three  }
  11.        Item       : string[20];         { numeric fields.              }
  12.        Quantity   : integer;
  13.        UnitPrice  : real;
  14.        VAT        : real;
  15.      end;
  16.  
  17. In the VAR declaration part of the program, specific instances of a
  18. record type are declared, as illustrated below:
  19.  
  20.   Var
  21.     Sale          : SaleType;        {An instance of the SaleType record.}
  22.  
  23. Such record-type variables can then be used like any other variable in the
  24. program, with access to an individual field by either of two methods:
  25.  
  26.   1. by entering the record variable name followed by a period and then
  27.      the individual field name, as illustrated below:
  28.  
  29.          Until .... (Sale.Name <> '') and (Sale.Name[0] < #50);
  30.  
  31.   2. by use of the WITH statement, described in the notes STRUCT.TXT
  32.      and illustrated below:
  33.  
  34.          procedure ReadRecord(Filename : string; Recpos : longint);
  35.          begin
  36.             ....
  37.             With Sale do
  38.             begin
  39.               ....
  40.               GoToXY(28,3);
  41.               ClrEol;
  42.               write(Name);
  43.               ....
  44.             end;
  45.          end;     {Proc ReadRecord}
  46.  
  47.  
  48. Record variables can be either global or local and may contain fields
  49. that are themselves records.
  50.  
  51. Records can contain a variant part, such that the field structure may
  52. be varied according to the resolution of a CASE statement ( see pages
  53. 31-2 of the Programmer's Guide).
  54.  
  55. Since records are usually associated with a database and the data is
  56. stored in a file on disk, the record-type variable is frequently
  57. assigned to a file by a declaration such as:
  58.  
  59.      SalesFile : File of SaleType;     { A file of SaleType records.}
  60.  
  61. Such files of records can be read from, or written to, by random access,
  62. using the Seek procedure (see below and page 122 of the Library Reference).
  63. By comparison, text files must be accessed sequentially.
  64.  
  65. File manipulation involves the procedures Assign, Rewrite, Reset, Read,
  66. Write and Close, which are described in the note FILES.TXT and illustrated
  67. by the procedures and statements used in the program RECORDS.PAS as below:
  68.  
  69.  procedure CreateFile(Filename : string);  { To create a new file on disk, }
  70.  begin                                     { open it by rewriting it, so   }
  71.     Assign(SalesFile,Filename);            { that there are no records     }
  72.     Rewrite(SalesFile);                    { preserved from a possible     }
  73.     Open := True;                          { previous file with same name. }
  74.  end;        {Proc CreateFile}
  75.  
  76.  procedure OpenFile(Filename : string);    { To open an existing file on   }
  77.  begin                                     { disk and reset it, so that    }
  78.     Assign(SalesFile,Filename);            { old records are preserved.    }
  79.     Reset(SalesFile);
  80.     Open := True;
  81.  end;        {Proc OpenFile}
  82.  
  83.  procedure ReadRecord(Filename : string; Recpos : longint);
  84.  begin
  85.  ....
  86.    Seek(SalesFile,RecPos);
  87.    Read(SalesFile,Sale);
  88.  ....
  89.  end;       {Proc ReadRecord}
  90.  
  91.  procedure AppendRecord(Filename : string);    { To append new data to the}
  92.  begin                                         { currently open disk file.}
  93.     Seek(SalesFile,FileSize(SalesFile));
  94.     Write(SalesFile,Sale);
  95.     ....
  96.  end;       {Proc AppendRecord}
  97.  
  98.  {Main program}
  99.  begin
  100.    ....
  101.    ....If Open = True then Close(SalesFile);
  102.    ....
  103.  end.
  104.  
  105. The procedure Seek moves the file pointer to the start of the record
  106. specified in the second parameter. The first parameter being the file
  107. variable name, as illustrated above and again below:
  108.  
  109.    procedure ChangeRecord(Filename : string; Recpos : longint);
  110.    begin
  111.       ....
  112.       Seek(SalesFile,Recpos);
  113.       write(SalesFile,Sale);
  114.       ....
  115.    end;
  116.  
  117. The program RECORDS.PAS is some 400 lines long because a number of
  118. procedures and statements are used to display information on the
  119. screen in windows and to access the disk to check the names of
  120. existing record files with extension names .REC, etc. However, the
  121. statements and procedures relating to records are adequately
  122. described above and should enable the reader to create a simple
  123. record program, especially if some of the procedures used in
  124. the present program RECORDS.PAS are 'copied' and 'pasted' to the
  125. new program using the ClipBoard. Transfer via the ClipBoard is
  126. achieved by highlighting the required code, using Ctrl-B at the
  127. beginning and Ctrl-K at the end and then using the Edit Menu and
  128. the Copy and Paste sub-menu options.
  129.  
  130.  
  131. Since a database of records will occupy a large amount of memory and
  132. there is need for the database to grow as required, dynamic memory
  133. allocation using the Heap is usually necessary (see the notes on the
  134. Heap and Pointers entitled HEAP&PTR.TXT).
  135.  
  136.  
  137. RECORDS.TXT
  138. 20.1.93
  139.  
  140.  
  141.  
  142.  
  143.  
  144.