home *** CD-ROM | disk | FTP | other *** search
- (* EXAMPLE4.PAS *)
-
- program driver;
- {$R+}
-
- uses
- FileBuff,
- FileDecs,
- Numbers;
-
- const
- TOTALRECORDS = 15; (* number of records in this example *)
- TESTSTRINGSIZE = 10; (* size of strings (part of test record) *)
-
- type
- TestString = String[TESTSTRINGSIZE];
-
- var
- dataFile : FnString; (* holds file name strings *)
- cnt : 1 .. TOTALRECORDS;
- str : TestString;
- thisFile : Text;
-
-
- (* This procedure creates the text file but does not write any data to it. *)
-
- procedure InitFiles;
-
- var
- thisFile : Text;
-
- begin
- dataFile := 'myFile1';
- RewriteTextFile(dataFile,thisFile);
- end;
-
-
- (* This routine creates a random string and is used for creating strings to
- demonstrate the handling of strings ina text file *)
-
- procedure CreateRandomString(var str : TestString);
-
- var
- chrCnt : 1 .. TESTSTRINGSIZE;
- tss : Byte;
-
- begin
- str := '';
- for chrCnt := 1 to TESTSTRINGSIZE do
- begin
- str[chrCnt] := Chr(Random(25) + 65);
- end;
- tss := TESTSTRINGSIZE;
- Move(tss,str,1);
- end;
-
-
-
- begin
-
- Writeln('Creating Text File ...'); (* just a note so you can
- follow along *)
-
- InitFiles; (* create the text file *)
-
-
- Writeln('Creating and storing data ... this may take a minute ...');
- (* just a note so you can
- follow along *)
-
- (* the following loop will create 15 records which will be inserted into
- the text file. *)
-
- for cnt := 1 to TOTALRECORDS do
- begin
- CreateRandomString(str);
- Writeln(str);
- OpenTextFile(dataFile,thisFile);
- Writeln(thisFile,str);
- Flush(thisFile); (* absolutely critical!! you must flush the
- data after each write operation or this
- doesn't work. If you are curious do some
- experiments. *)
- end;
-
- Writeln;
- Writeln;
-
- CloseFile(dataFile);
-
- for cnt := 1 to TOTALRECORDS do
- begin
- CreateRandomString(str);
- Writeln(str);
- AppendTextFile(dataFile,thisFile);
- Writeln(thisFile,str);
- Flush(thisFile); (* absolutely critical!! you must flush the
- data after each write operation or this
- doesn't work. If you are curious do some
- experiments. *)
- end;
-
-
- Writeln;
- Writeln;
-
- str := '';
-
- CloseFile(dataFile); (* Close the file *)
-
- OpenTextFile(dataFile,thisFile); (* Open the file in output mode *)
-
- while not Eof(thisFile) do
- begin
- Readln(thisFile,str);
- Writeln(str);
- end;
-
- Writeln('Closing Files ...'); (* just a note so you can
- follow along *)
-
- CloseAllFiles; (* Close the files to clean up. *)
-
-
- end.