yuyoyuppe
yuyoyuppe

Reputation: 1602

pascal crt output redirection

This is a strange question. I have pascal source which intesively uses GotoXY function for output generation, but now I need to redirect output from console to a file. Any ideas?

Upvotes: 0

Views: 345

Answers (2)

yuyoyuppe
yuyoyuppe

Reputation: 1602

Finally done.

uses
  Windows;
type
 TAttachConsole = function (dwProcessId: DWORD): LongBOOL stdcall;

var
 AttachConsole: TAttachConsole;
 mProcessID, Hcwnd: Cardinal;

procedure Attach;
begin
 @AttachConsole := GetProcAddress(GetModuleHandle('kernel32.dll'), 'AttachConsole');
 GetWindowThreadProcessId(FindWindow(nil,'C:\WINDOWS\system32\cmd.exe'),@mProcessID);
 AttachConsole(mProcessID);
end;

function get(x, y: byte) : string;
const
 SMB = 1;
var
 chRead: Cardinal;
 BufInfo: _CONSOLE_SCREEN_BUFFER_INFO;
 lpCh : PChar;
 Coord: _COORD;
begin
   Hcwnd:=GetStdHandle(STD_OUTPUT_HANDLE);
   GetConsoleScreenBufferInfo (Hcwnd, BufInfo);
   GetMem(lpCh,SMB);
   Coord.X:=x;
   Coord.Y:=y;
   lpch := '';
   ReadConsoleOutputCharacter(Hcwnd,lpCh,SMB,Coord,chRead);
   get:=string(lpCh^);
end;

var x, y : integer;
buf : array[0..24] of string;
begin
for y := 0 to 24 do
begin
  for x := 0 to 79 do
    buf[y] := buf[y] + get(x,y);
end;
for y := 0 to 24 do
writeln(buf[y]);
end.

Upvotes: 1

Hot Licks
Hot Licks

Reputation: 47749

I assume you're talking about the Turbo Pascal function, and not the similarly-named function available for C/C++. (Though the functions are very similar.)

Unfortunately, you have several things working against you -- the screen and file versions of Writeln are different, GotoXY is a reserved word, et al. I suspect that your best bet is to replace I/O calls with a common function call that internally directs to screen or file based on a global flag. Or you could insert individual IF statements to select which logic to use everywhere I/O occurs.

Upvotes: 0

Related Questions