Back to DFS's Pascal Page


FILES II - Names as Text

The program below processes string data, making use of a text file.

You can create a data file using the Turbo Pascal editor. Hit the ENTER key at the end of each string. This will cause each line in the file to be terminated by hexadecimal 0D 0A. Create a test file with the names of at least five (5) of your classmates and save it as names.dat.

Program readlist opens a file called names.dat, reads the strings found there, and stores them in an array.

program readlist (output);
const
  Max = 20;
var
  infile: Text;
  names : array [1..Max] of string;
  i, numnames : integer;
begin
  for i := 1 to Max do
     names[i] := '';
  Assign(infile, 'names.dat');
  Reset(infile);
  numnames := 0;
  while (not Eof(infile)) do
     begin
        numnames := numnames + 1;
        Read(infile, names[numnames]);
        Readln(infile);  {get past EOL}
     end;
  Close(infile);
  for i := 1 to numnames do
     writeln(names[i])
end.

© 1999 DFStermole
HTMLified: 29 Dec 99