A r t i c l e s
Navigation

Note: This Wiki is
outdated, personal views
may have changed.
L505 A.I. bot is dead
long live THX 1138

M a i n P a g e

D i r e c t o r y

Redirecting STDOUT To Text File


Redirecting standard out on the command line to a text file is not so hard if you know about the OUTPUT global variable which you can take control of.
program project1;

{$mode objfpc}{$H+}

var
  RedOutFile: text;

procedure RedirectSTDOUT(FileName: string);
begin
//  close(OUTPUT); //closes STDOUT, actually doesn't seem to be needed. 
  Assign(OUTPUT, FileName); // now assign a file to STDOUT (redirect)
  Rewrite(OUTPUT); // open file for writing
end;

procedure RestoreSTDOUT;
begin
  close(output); // close file
  Assign(output,''); // restore STDOUT mode
  rewrite(output); // open STDOUT for writing
end;

begin
  WriteLn('This text is written to console');
  RedirectStdOut('Error.txt');
  writeln('This text is written to Error.txt');

  // want to restore std out?
  RestoreStdOut;
  WriteLn('Back to console again');
  readln;
end.


About
Note: This Wiki is outdated, personal views may have changed.

This wiki contains info on life, health, humans, nature, programming, database, fads, paradigms, poems, principles, theories.

Articles may contain statements which some may find helpful and encouraging, or even discouraging.

Beware, I believe in the Grand Justice system.
_ _ _