home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TBTREE.ZIP / EXAM4.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-03-13  |  3.3 KB  |  126 lines

  1. (* EXAMPLE4.PAS *)
  2.  
  3. program driver;
  4. {$R+}
  5.  
  6. uses
  7.     FileBuff,
  8.     FileDecs,
  9.     Numbers;
  10.  
  11. const
  12.     TOTALRECORDS = 15;                (* number of records in this example *)
  13.     TESTSTRINGSIZE = 10;          (* size of strings (part of test record) *)
  14.  
  15. type
  16.     TestString = String[TESTSTRINGSIZE];
  17.  
  18. var
  19.     dataFile  : FnString;                       (* holds file name strings *)
  20.     cnt : 1 .. TOTALRECORDS;
  21.     str : TestString;
  22.     thisFile : Text;
  23.  
  24.  
  25. (* This procedure creates the text file but does not write any data to it.   *)
  26.  
  27. procedure InitFiles;
  28.  
  29. var
  30.     thisFile : Text;
  31.  
  32.     begin
  33.     dataFile := 'myFile1';
  34.     RewriteTextFile(dataFile,thisFile);
  35.     end;
  36.  
  37.  
  38. (* This routine creates a random string and is used for creating strings to
  39. demonstrate the handling of strings ina text file                            *)
  40.  
  41. procedure CreateRandomString(var str : TestString);
  42.  
  43. var
  44.     chrCnt : 1 .. TESTSTRINGSIZE;
  45.     tss : Byte;
  46.  
  47.     begin
  48.     str := '';
  49.     for chrCnt := 1 to TESTSTRINGSIZE do
  50.         begin
  51.         str[chrCnt] := Chr(Random(25) + 65);
  52.         end;
  53.     tss := TESTSTRINGSIZE;
  54.     Move(tss,str,1);
  55.     end;
  56.  
  57.  
  58.  
  59. begin
  60.  
  61. Writeln('Creating Text File ...');                  (* just a note so you can
  62.                                                       follow along           *)
  63.  
  64. InitFiles;                                           (* create the text file *)
  65.  
  66.  
  67. Writeln('Creating and storing data ... this may take a minute ...');
  68.                                                    (* just a note so you can
  69.                                                       follow along           *)
  70.  
  71.     (* the following loop will create 15 records which will be inserted into
  72.        the text file.                                                        *)
  73.  
  74. for cnt := 1 to TOTALRECORDS do
  75.     begin
  76.     CreateRandomString(str);
  77.     Writeln(str);
  78.     OpenTextFile(dataFile,thisFile);
  79.     Writeln(thisFile,str);
  80.     Flush(thisFile);             (* absolutely critical!! you must flush the
  81.                                     data after each write operation or this
  82.                                     doesn't work.  If you are curious do some
  83.                                     experiments.                             *)
  84.     end;
  85.  
  86. Writeln;
  87. Writeln;
  88.  
  89. CloseFile(dataFile);
  90.  
  91. for cnt := 1 to TOTALRECORDS do
  92.     begin
  93.     CreateRandomString(str);
  94.     Writeln(str);
  95.     AppendTextFile(dataFile,thisFile);
  96.     Writeln(thisFile,str);
  97.     Flush(thisFile);             (* absolutely critical!! you must flush the
  98.                                     data after each write operation or this
  99.                                     doesn't work.  If you are curious do some
  100.                                     experiments.                             *)
  101.     end;
  102.  
  103.  
  104. Writeln;
  105. Writeln;
  106.  
  107. str := '';
  108.  
  109. CloseFile(dataFile);                                       (* Close the file *)
  110.  
  111. OpenTextFile(dataFile,thisFile);             (* Open the file in output mode *)
  112.  
  113. while not Eof(thisFile) do
  114.     begin
  115.     Readln(thisFile,str);
  116.     Writeln(str);
  117.     end;
  118.  
  119. Writeln('Closing Files ...');                      (* just a note so you can
  120.                                                       follow along           *)
  121.  
  122. CloseAllFiles;                               (* Close the files to clean up. *)
  123.  
  124.  
  125. end.
  126.