TURBO PASCAL TEXT FILE INPUT/OUTPUT

Turbo Pascal will allow you to read input data from a text file and
send your output to a text file.  By "text file" we simply mean a
sequence of alphanumeric characters; Turbo will treat them as if
you typed them in from the keyboard!  You can prepare an input data
file with the Turbo Editor (in fact, this is the recommended way to
do it).  I will program data files on the Web server (point to the
ROAD), you can download them to a diskette and your programs can
process the data before submitting them to be graded.  You can send
your output to a text file on your diskette.  The Turbo Editor can
then read the file and print it!

Study the following example program to see (in general) how to read
from an input file and send output to a file.  REMEMBER: the words
"input" and "output" are always with respect to the program
processing the data!  Thus an input file provides a program with
data to process.  An output file receives the data processed by the
program.  Also remember that Pascal must always start at the
beginning of both input and output text files; you can't jump
around in them.

The input data file, named EMPLOYEE.DAT, will be processed by the
following program. 

{ Input file for FILE_IO.PAS }
John Smith                    10000.00
Alice Johnson                 20000.00
Steven King                   40000.00
Sol Bellow                    30000.00
Norman Mailer                 50000.00

================================================================
Program FileIO;
{ Text File IO demonstration.
  FILENAMES:  FILEIO.PAS, EMPLOYEE.DAT }

Uses
  Crt;

Const
  Infile_name = 'EMPLOYEE.DAT';   { DOS file names }
  Outfile_name = 'TABLE.OUT';
  Banner = '=================================================';

Var
  Entry: Integer;                 { input storage }
  Employee_name: String[30];      { storage for employee name }
  Count: Integer;                 { output & processing storage }
  Salary, Sum, Average: Real;     { input & processing storage }
  Infile, Outfile: Text;          { text file "windows" }

Begin
  Clrscr;


  { Open input file for processing }

  Writeln('Input coming from text file ', Infile_name); 
  Assign(Infile, Infile_name);
  Reset(Infile);


  { Prepare output file for results }

  Writeln('Output going to text file ', Outfile_name);  
  Assign(Outfile, Outfile_name);
  Rewrite(Outfile);

  Writeln(Outfile, Banner);
  Writeln(Outfile, 'EMPLOYEE DATA:');
  Writeln(Outfile, Banner);
  Writeln(Outfile, 'NAME' :6, 'SALARY' :30);
  Writeln(Outfile, Banner);
  Readln(Infile);  { ignore the first line of input file }

  Sum := 0.0;      { initialize processing variables }
  Count := 0;

  WHILE NOT EOF(Infile) DO
    Begin
      Readln(Infile, Employee_name, Salary);
      Sum := Sum + Salary;
      Count := Count + 1;
      Writeln(Outfile, Employee_name :30, Salary :0:2);
    End;

  Close(Infile);        { processing of the input file complete }

  Writeln(Outfile, Banner);
  Writeln(Outfile, 'There were ', Count, ' employees processed.');
  Average := Sum / Count;
  Writeln(Outfile, 'The average salary is ', Average :0:2);
  Writeln(Outfile, Banner);

  Close(Outfile);           { processing complete in output file }
  Writeln('All files closed and processing complete.');
  Readln
End.

-------------------------------------------------------------------
This is what is seen on the user's screen during processing:
-------------------------------------------------------------------
Input coming from text file EMPLOYEE.DAT
Output going to text file TABLE.OUT

All files closed and processing complete.

-------------------------------------------------------------------
This is what is sent to the output file TABLE.OUT when the program
runs:
-------------------------------------------------------------------

=================================================
EMPLOYEE DATA:
=================================================
  NAME                        SALARY
=================================================
John Smith                    10000.00
Alice Johnson                 20000.00
Steven King                   40000.00
Sol Bellow                    30000.00
Norman Mailer                 50000.00
=================================================
There were 5 employees processed.
The average salary is 30000.00
=================================================