Hi Patrick, didn't realize that your question hadn't been answered.
You're going to kick yourself if you haven't already :-)
Guess you missed the fact than each call to onoff() did a file assign and a
reset/rewrite. Not only has any previously opened file handle been tossed
in the bit bucket, but the new files are always reset to a {*word*269} state :->
Unless you take special steps, once a text file is opened it has to remain
open until you are finished with it. Otherwise you lose the current file
position, contents, etc.
To solve your problem you could define a pair of file pointers and an extra
pair of text file variables. Then initialize the new set of files if the /r
option was specified and load the pointers with their address otherwise load
the pointers with the address of the default input and output files
variables.
This solution allows you to use all CRT functions and normal read/write
readln/writeln with the CRT, and to blindly use the pointers to do the basic
i/o.
I've modified your 2nd program with a variant of the above that is even
simpler and handles any combination of input/output redirection. This allows
you to selectively choice to redirect input, output, both, or neither. And
this one doesn't need the /r parameter. Just do it!
The reason it works is in the way a TFDD (Text File Device Diver) is
implemented. In this case the CRT unit. You could just as well have used
Assign(InpF, ParamStr(1)) and Assign(OutF, ParmStr(2)); If the parameters
don't exist, everything works as described, if they do exist, the respective
file specs are used. Besides providing an alternative method of specifying
the i/o files, using ParamStr() allows debugging file i/o from the IDE..
Quote
In article <4lc67p$1...@trumpet.aix.calpoly.edu> Patrick D Rockwell wrote:
>Date: 20 Apr 1996 19:23:53 -0700
>From: prock...@trumpet.aix.calpoly.edu (Patrick D Rockwell)
>Newsgroups: comp.lang.pascal.borland,comp.lang.pascal.misc
>Subject: I/O redirection while using CRT unit.
[snip - everything vaporised and replaced by a modified program}
program red;
uses crt,files;
var i,j,k,m,n:integer;
ww:word;
r:char;
InpF: Text; { <--<< new parameters }
OutF: Text;
begin
textcolor(3);textbackground(5);
write('Press any key to continue!');
textcolor(7);textbackground(0);
writeln;
Assign(InpF, ''); Reset(InpF); { Initialize redirect }
Assign(OutF, ''); Rewrite(OutF); { or CRT i/o ...red }
for i:=1 to 3 do begin
for j:=1 to 3 do begin
textcolor(2);
writeln('Enter two integers'); { <--<< to CRT only! }
readln(InpF, m,n); { <--<< from redirect/CRT }
k:=m+n;
writeln(OutF, m, ' + ',n,' = ',k);{ <--<< to redirect/CRT }
end;
end;
{ And a little clean up ...red}
Close(InpF);
Close(OutF);
end.
----------------------------------------------------------------
Did I just perform surgery on myself? :->
...red
Knowledge is one of the few things that you
can give away and still keep for yourself.