Board index » delphi » I/O redirection while using CRT unit.

I/O redirection while using CRT unit.

I have a question. I've written two programs. Here is one of them.

--------------------------------------------------------
program red;
uses crt;
procedure onoff(d:integer); {Toggle crt mode}
begin
  if d=1 then begin {Shut off crt mode}
    assign(input,'');reset(input);
    assign(output,'');rewrite(output);
  end;
  if d=2 then begin {Turn on crt mode}
    assigncrt(input);reset(input);
    assigncrt(output);rewrite(output);
  end;
end;
var i,j,k,m,n:integer;
    ww:word;
    r:char;
begin
  textcolor(3);textbackground(5);
  write('Press any key to continue!');
  textcolor(7);textbackground(0);
  writeln;
  if(paramstr(1)<>'/r') then onoff(2);
  if(paramstr(1)<>'/r') then r:=readkey;
  if(paramstr(1)='/r') then onoff(1);
  for i:=1 to 3 do begin
    for j:=1 to 3 do begin
      textcolor(2);
      writeln('Enter two integers');
      readln(m,n);
      k:=m+n;
      writeln(m, ' + ',n,' = ',k);
    end;
  end;
end.
--------------------------------------

If you type red/r<file1.txt>file2.txt if takes the contents of file1.txt,
processes them and puts the result in file2.txt. If you ommit the '/r'
parameter, it cant do this because it won't shut off the crt unit. The
procedure "onoff(d)" will turn the crt unit off if d=1 and on if d=2.
the input file that I used looks like this.

1 2
1 3
1 4
4 5
3 6
3 0
4 8
2 10
1 9

and the output results look like this.

Enter two integers
1 + 2 = 3
Enter two integers
1 + 3 = 4
Enter two integers
1 + 4 = 5
Enter two integers
4 + 5 = 9
Enter two integers
3 + 6 = 9
Enter two integers
3 + 0 = 3
Enter two integers
4 + 8 = 12
Enter two integers
2 + 10 = 12
Enter two integers
1 + 9 = 10

As you can see, this program runs fine! But here is a similar copy of the
program, with just  two different lines.

--------------------------------------------------------

program red;
uses crt;
procedure onoff(d:integer); {Toggle crt mode}
begin
  if d=1 then begin {Shut off crt mode}
    assign(input,'');reset(input);
    assign(output,'');rewrite(output);
  end;
  if d=2 then begin {Turn on crt mode}
    assigncrt(input);reset(input);
    assigncrt(output);rewrite(output);
  end;
end;
var i,j,k,m,n:integer;
    ww:word;
    r:char;
begin
  textcolor(3);textbackground(5);
  write('Press any key to continue!');
  textcolor(7);textbackground(0);
  writeln;
  if(paramstr(1)<>'/r') then onoff(2);
  if(paramstr(1)<>'/r') then r:=readkey;
  if(paramstr(1)='/r') then onoff(1);
  for i:=1 to 3 do begin
    for j:=1 to 3 do begin
      textcolor(2);
      onoff(2);                      <----"Extra line #1."
      writeln('Enter two integers');
      onoff(1);                      <----"Extra line #2."
      readln(m,n);
      k:=m+n;
      writeln(m, ' + ',n,' = ',k);
    end;
  end;
end.
----------------------------------------------------------------

And here is out the result looks!

0 + 0 = 0

When I run this second program using an input file but without directing
the output to a file with the greater than sign, it prints 9 lines, but
only the first one is right. The remaining 8 are just like the line
above. Also, when I do redirect this second program to an output file,
it only prints the above line, which is incorrect. Can anyone help me?
I want to be able to turn the CRT unit on and off at will so that I can
have a program which writes color text while reading from a text file.
Can anyone help me? Thanks in advance.

--
..........Patrick D. Rockwell..................................................
Internet: prock...@trumpet.calpoly.edu
Prodigy: hnhc85a
America On-Line: patri48975

 

Re:I/O redirection while using CRT unit.


Quote
Patrick D Rockwell wrote:
> I want to be able to turn the CRT unit on and off at will so that I can
> have a program which writes color text while reading from a text file.

Try:

program MyProg;
uses CRT;
begin
  TextColor(red);
  WriteLn('This is in red on the screen.');
  Assign(output,''); rewrite(output);
  WriteLn('This goes to StdOut');
  AssignCRT(output); rewrite(output);
  WriteLn('This is in red on the screen again.');
end.

Then run,
  myprog >result.txt
and see what happens.

     - Rich

Re:I/O redirection while using CRT unit.


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.

Re:I/O redirection while using CRT unit.


In article <4lgllt$...@news-e2c.gnn.com>,
R. E. Donais  <RDon...@gnn.com> wrote:

Quote
>Hi Patrick, didn't realize that your question hadn't been answered.

>You're going to kick yourself if you haven't already :-)  

Hi! Thanks. I should have mentioned this before, in my post but I'm
using version 4.0 which is ANCIENT. But I will try your suggestion and
see what happens. I had a feeling yesterday that what you said in your
post was the problem. Thanks again!

--
..........Patrick D. Rockwell..................................................
Internet: prock...@trumpet.calpoly.edu
Prodigy: hnhc85a
America On-Line: patri48975

Other Threads