(*
filename: fileTest1.pp *)
program fileTest (fin, fout, myfout, input,
output);
{
This program shows how to declare files and
how to read
from and write to
them. Although you may define file types
you
}
var fin , fout,
myfout : textfile;
myFileName : string;
x, y
: integer;
begin
{
The assign statement links the internal file name with a
disk file name. file names need to be in single quotes
These assign statements do not indicate
whether the
files will be read
from or written to. They just make
the connections.
}
assign (fin,
'data.in');
assign (fout,
'data.out');
{
reset opens a file we want to read from.
It should exist before program is run.
}
{
rewrite opens a file we want to write to
It will be created if it doesn't
exist.
It will be written over if it does
exist.
Both statements move the file
pointer to the
beginning
of the file.
}
reset (fin);
rewrite (fout);
{
In this class you should always ask the user for
the name of the input file. If
the user does not
provide a full path,
the program assumes the file
is in the directory
where the executable is.
}
writeln ('please
enter the name of your output file with complete path' );
readln (myFileName);
assign (myfout ,
myFileName); { links internal filename
with diskfile name
provided
by user
}
x := 379;
write (fout, x, '
');
writeln (fout, x);
read(fin, y);
readln (fin, y);
writeln (fout, ' x
is ', x);
close(fin); (* programmer should close files although
operating system may do it *)
close(fout); (* if
you forget. Do not count on it. '*)
writeln (' this
statement appears on the screen');
rewrite (myfout);
writeln (myfout, '
this statement appears in the file ' + myfilename );
writeln (' have a look
in file ' , myfilename);
close (myfout);
end.