如何读取UTF8编码的INI文件? [英] How do I read a UTF8 encoded INI file?

查看:1220
本文介绍了如何读取UTF8编码的INI文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UTF-8格式的INI文件。



我使用Delphi 2010读取INI文件,并使用INI文件中的值填充TStringGrid 。

  var 
ctr:Integer;
AppIni:TIniFile;
begin
AppIni:= TIniFile.Create(ExtractFilePath(Application.ExeName)+'test.ini');
for ctr:= 1 to StringGrid1.RowCount do begin
StringGrid1.Cells [0,ctr]:= AppIni.ReadString('Column1','Row'+ IntToStr(ctr),'');
StringGrid1.Cells [1,ctr]:= AppIni.ReadString('Column2','Row'+ IntToStr(ctr),'');
结束
AppIni.Free;

问题是,unicode字符出现在显示2个字符的TStringGrid中,而不是1个unicode



如何解决这个问题?

解决方案

code> TIniFile 类是INI文件的Windows API的包装器。这确实支持Unicode INI文件,但只有这些文件被编码为UTF-16。 Michael Kaplan在这里有更多的细节: Unicode INI函数; Unicode INI文件?



所以,你没有运气与 TIniFile 。相反,您可以使用 TMemIniFile ,它允许您在其构造函数中指定编码 TMemIniFile 类是INI文件支持的本机Delphi实现。两班之间有各种利弊。在您的情况下,只有 TMemIniFile 可以满足您的需求,所以看起来其优点将超过其缺点。


I have an INI file in UTF-8 format.

I am using Delphi 2010 to read the INI file and populate a TStringGrid with the values in the INI file.

var
  ctr : Integer;
  AppIni : TIniFile;
begin
  AppIni := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'test.ini');
    for ctr := 1 to StringGrid1.RowCount do begin
        StringGrid1.Cells[0,ctr] := AppIni.ReadString('Column1','Row'+IntToStr(ctr),'');
        StringGrid1.Cells[1,ctr] := AppIni.ReadString('Column2','Row'+IntToStr(ctr),'');
    end;
  AppIni.Free;

The problem is that the unicode characters are appearing in the TStringGrid displaying 2 characters, rather than the 1 unicode character.

How do I resolve this?

解决方案

The TIniFile class is a wrapper of the Windows API for INI files. This does support Unicode INI files, but only if those files are encoded as UTF-16. Michael Kaplan has more details here: Unicode INI function; Unicode INI file?

So, you are out of luck with TIniFile. Instead you could use TMemIniFile which allows you to specify an encoding in its constructor. The TMemIniFile class is a native Delphi implementation of INI file support. There are various pros and cons between the two classes. In your situation, only TMemIniFile can serve your needs, so it's looking like its pros are going to outweigh its cons.

这篇关于如何读取UTF8编码的INI文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆