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
it is safer to
declare them as textfile
}
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);
writeln (' got here 1 ');
{
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);
writeln ('*',
myfilename, '*');
assign (myfout ,
myFileName);
(*
links internal filename with diskfile name provided by user *)
x := 379;
write (fout, x, '
');
writeln (fout, x);
writeln (' got here
2 ');
read(fin, y);
writeln (y);
writeln (' got here
3 ');
readln (fin, y);
writeln (' got here
4 ' );
writeln ( y);
writeln (fout, ' x
is ', x);
writeln (' got here
5 ');
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.