[fpc-pascal]Redirecting StdOut

Rich Pasco pasco at acm.org
Sat May 25 00:05:21 CEST 2002


Hi,

Here's a simple Win32 console program which is supposed to demonstrate
redirecting StdOut.  However the line "This should go to redout.txt"
still goes to to the console.  Can anyout tell me what is wrong?

     - Rich

uses Windows, DOS;

var
  OutHandle: handle; { Handles identify stored file table entries }

function RedStdOut(var OutFile : text) : boolean;
  begin
    OutHandle := GetStdHandle(STD_OUTPUT_HANDLE);
    RedStdOut := SetStdHandle(STD_OUTPUT_HANDLE,
TextRec(OutFile).handle);
  end;

function UnRedStdOut : boolean;
  begin
    UnRedStdOut := SetStdHandle(STD_OUTPUT_HANDLE, OutHandle);
  end;

var
  RedOutFile: text;

procedure RedOut(ToFileName: string);
  begin
    Assign(RedOutFile,ToFileName);
    Rewrite(RedOutFile);
    if not RedStdOut(RedOutFile) then
      WriteLn('Could not redirect StdOut to '+ToFileName);
  end;

procedure UnRedOut;
  begin
    if not UnRedStdOut then WriteLn('Could not restore StdOut.');
    close(RedOutFile);
  end;

begin
  WriteLn('This goes to console');
  RedOut('redout.txt');
  writeln('This should go to redout.txt');
  UnRedOut;
  WriteLn('This goes to console again');
end.





More information about the fpc-pascal mailing list