[fpc-pascal] Problems with writing to console

James Richters james at productionautomation.net
Wed Jan 10 04:27:59 CET 2018

I suspect the output when writing to StdOut is moving the cursor to a location that is not known or kept track of by regular writeln in the CRT unit.  If this is what's causing seemingly random positioning,   You could possibly keep track of where the cursor is supposed to be yourself with the CRT functions WhereX32 & WhereY32 then move the cursor to  a fixed position with the CRT function GotoXY32 , perform the Writeln to StdOut, then move the cursor back to the position saved by WhereX32/WhereY32 with another GotoXY32.  You mentioned you have several applications that work together, so maybe you can somehow use these functions to keep things straight in the console window, by possibly parking the cursor in a known position and relaying the position to the other program.

One problem with using WhereX32, WhereY32, and GotoXY32 with windows consoles, is that the console window is not a predetermined shape, it depends on how the user has the system configured. You can get around this two ways:

1. by detecting the existing shape of the console window with GetConsoleScreenBufferInfo()   Now you will know the shape of the console window and stay within it.
2. force the windows console window to be  particular shape with SetConsoleWindowInfo();  and you can also force a particular console screen buffer with SetConsoleScreenBufferSize();   I believe there is also a way to force the font, and font size for windows consoles as well.

I've attached a CRT Demo program that uses the above functions as an example.

It's not clear If you are using StdOut for redirection purposes, however if the output is redirected, none of this will work, because as Michael mentioned, CRT with redirection is not supported.. if you try it, StdOut is all that is redirected and Output is just lost. so then you don't have anything on the screen and none of the positioning will mean anything.   I did notice you can also write stuff to StdErr and it will redirect separately from output with 2> but if you use 2>&1 then you get no screen output.  So if you did want some stuff on the screen and to redirect other stuff, then the stuff you want redirected could be done with stderr and you could also still use positioning functions for normal output.  I attached another sample program that demonstrates this, try to run it with various redirects and it will be clear what is redirected and what isn't  It also shows writing things to stderr then using gotoxy32 to write over it so it won't even show up on the screen. If the program is run without redirection. (set wait4key to false to see this)


-----Original Message-----
From: fpc-pascal [mailto:fpc-pascal-bounces at lists.freepascal.org] On Behalf Of Michael Van Canneyt
Sent: Tuesday, January 09, 2018 7:16 PM
To: FPC-Pascal users discussions <fpc-pascal at lists.freepascal.org>
Subject: Re: [fpc-pascal] Problems with writing to console

On Tue, 9 Jan 2018, Darius Blaszyk wrote:

> Hi,
> I have been mixing "writeln(StdOut" and regular writeln in my 
> application (actually a couple of applications working together) and 
> found that the output on the console is mangled when I do that. Output 
> is being overwritten and placed semi randomly on the console. What is 
> the standard output for writeln? I thought it would be StdOut as the 
> name suggests. Using StdOut at the same time will not allow colors
> (TextColor) from the crt unit to be shown on the console. On the other 
> hand reading output from the console does require writing to StdOut.

Take care, stdout and output are not the same text file.

if no file is specified, then output is assumed.

The crt unit only redirects output. not stdout.
(See around line 438 of crt.inc)

using crt and output redirection together is not supported.

fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Console_Size_Detect_Demo.pas
Type: application/octet-stream
Size: 4413 bytes
Desc: not available
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20180109/cd059d66/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.pas
Type: application/octet-stream
Size: 931 bytes
Desc: not available
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20180109/cd059d66/attachment-0001.obj>

More information about the fpc-pascal mailing list