Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2002-05

Redirecting StdOut

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Rich Pasco

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.
Rich Pasco

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.
Andreas K. Foerster
to Rich Pasco: Here's a simple Win32 console program which is supposed to demonstrate redirecting…
Rich Pasco wrote 5 lines
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?

I don't understand that windows commands, but input and output are done using the variables INPUT, OUTPUT, STDERR. These have to be changed in my opinion.
This would also be a more portable way.

Tschuess Andreas
Unknown author
to Rich Pasco: Here's a simple Win32 console program which is supposed to demonstrate redirecting…
Rich Pasco wrote 5 lines
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?

I think the problem is that the 'old' standard handle is still in use after you switched the handle. The Output file variable still uses the old value.

It would be easier to simply do

  close(output);
  Assign(output,'somefile.txt');
  rewrite(output);
restoring it is then
  close(output);
  Assign(output,'');
  rewrite(output);

Michael.