Talk:Using TIniFile and TMemIniFile

From RAD Studio
Jump to: navigation, search

I found a woraround to a problem, but I am not sure if this should be added to the docs and if is even legal without pitfalls:

Q: How to write UTF8 string to an INI file

I am new to Delphi 2010, and wonder how I could write UTF8 texts to IniFiles. My UTF8String data gets converted to Unicode and then to AnsiString when writing to a plain (non-Unicode) INI file. I would like to retain non-Unicode notepad-like INI, and encode Unicode with UTF8, but am not sure how to go around the conversion.

Answer (written by a beginner)

ShortString typecast will make it sure individual UTF-8 codes get written to a single-byte-character INI text file:


var US:UTF8String;
      INI:TIniFile;
begin
   with INI do begin
       WriteString('A','B',ShortString(US));
       US:=ShortString(ReadString('A',B','Default'));
    end;
end;