[fpc-devel] TStringList.LoadFromFile and SavetoFile - file encoding support

Vincent Snijders vsnijders at vodafonevast.nl
Tue Feb 3 08:02:30 CET 2009

Graeme Geldenhuys schreef:
> Hi,
> I just read all the comments about the following bug report in filed
> under the Lazarus project.
>   http://bugs.freepascal.org/view.php?id=12676
> The comments posted doesn't seem sufficient to me.  If a user selects
> a file to be loaded, they have no clue if that file is ANSI, UTF-8,
> UTF-16 etc encoded. The suggestion by the Lazarus developers is to
> ALWAYS assume the file is in UTF-8 (just because LCL uses UTF-8
> internally) and to do a UTF8Encode on each line of the file. So what
> happens if you do a .SavetoFile(...)?  Must you UTF8Decode each line
> again??

I am a Lazarus developer, and I don't think I said it like that.

What I mean is:
If you load a file using LoadFromFile, the lines of the file are loaded 
in ansistrings. No conversion is done by the RTL, so the encoding 
remains the same as is in the file.

Now, the LCL is very picky about its encoding, it wants always UTF8 
encoded strings. It is not a chameleon like that RTL that changes its 
encoding according to the systems settings. If you want to show strings 
loaded by LoadFromFile in a LCL control, you need to make sure they are 
valid UTF8 strings. And honestly, it is only you who make sure that it 
is, because you know the initial encoding.


More information about the fpc-devel mailing list