I use FPC 3.0.4 on Linux (Ubuntu 18.04). If you run the following code in a terminal console, you'll see 30 lines of 100 chars each:
program demo;
{$mode objfpc}{$H+}
uses crt;
var s: string[255];
i: integer;
begin
s:=''; for i:=1 to 10 do s:=s + '1234567890';
for i:=1 to 30 do writeln(i:2, ' ', s);
end.
But if you redirect the output like
demo > outfile.txt
you'll have 2 strange things in your file (see attached screenshot):
- each line has wrongly inserted a Linefeed ($0A) after column 80
- from line 13 on, each line is followed by an empty line, because it ends with $0A0D instead of $0A
On Windows this problem does not exist, only on Linux. I found out, that the "culprit" ist unit crt. If you omit unit crt, the problem disappears. But my "real program" uses a couple of units, which directly or indirectly use unit crt, so I can't avoid unit crt.
Question #1: is this a bug or "as designed"?
To avoid the problem I tried:
begin
crt.WindMaxX:=2000;
crt.WindMaxY:=dword(-1);
s:=''; for i:=1 to 10 do s:=s + '1234567890';
for i:=1 to 30 do writeln(i:2, ' ', s);
end.
The redirected output now is correct, but now I get 2 Runtime errors:
hg6@i3300:~$ demo > outfile.txt
Runtime error 216 at $0805D8D4
$0805D8D4
$0806EC5D
Runtime error 216 at $0805D888
$0805D888
$0806EC5D
Runtime error 216 means "General Protection fault. The application tried to access invalid memory space."
Question #2: does anybody know how to avoid these runtime errors or knows another way to avoid the $0A and $0D in the redirected output? Thanks in advance.