[fpc-devel] Console encoding on Windows, output to file with ">"

Ondrej Pokorny lazarus at kluug.net
Wed May 3 18:18:53 CEST 2017


Michael, because I cannot comment on a closed issue report I post my 
answer to https://bugs.freepascal.org/view.php?id=31746#c100091 here:

 > I don't think what you write is correct. As far as I can see in the 
code of Delphi Berlin: Delphi uses always the defaultsystemcodepage for 
text files, regardless of what the file is used for.

What do you mean? I checked both Berlin and Tokyo and in "function 
TextOpen(var t: TTextRec): Integer;" in unit System you see that if the 
text file is used for console input/output the CodePage is 
GetConsoleOutputCP/GetConsoleCP whereas if the text file is used for 
other purposes it uses DefaultSystemCodePage. (Search for "if 
GetFileType(t.Handle) = 2 then"; 2=FILE_TYPE_CHAR).

It is correct that the patch doesn't exactly reflect Delphi behavior - 
it sets the DefaultSystemCodePage only for standard output and not all 
text files. It mises the Delphi fallback to DefaultSystemCodePage.

 > FPC uses a different mechanism which allows you to write files in a 
different code page.

Exactly. The issue report is about the fact that the mechanism is 
different - which puzzled me a lot. I just didn't understand why I see 
nonsense in Notepad when I open the output from an FPC application 
whereas output from Delphi shows fine.

Some background: I convinced one of my customers that FPC is better than 
Delphi (I have known it for a long time already) and migrated one of my 
console programs for him from Delphi to FPC and I didn't understand why 
FPC "corrupts" the output to file whereas the output to console is OK. I 
wasn't aware that the FPC output was in console codepage even if you 
redirect the output to a file (you don't really use console codepage for 
files, don't you).

I accept that this behavior is by design and wanted - I hadn't found any 
information on this Delphi incompatibility before I started checking the 
issue. So at least the issue report can be some kind of information 
source even if you won't fix it - hopefully Google files it correctly :)

 > You can do your own detection and set the code page accordingly

This is exactly what I found out and what I posted as workaround in 
"Steps To Reproduce" :)

Ondrej



More information about the fpc-devel mailing list