TMemIniFile.Create中的德语Umlaut字符异常 [英] Exception with German Umlaut characters in TMemIniFile.Create

查看:118
本文介绍了TMemIniFile.Create中的德语Umlaut字符异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.URL文件,其中包含以下文本,其中包含德语Umlaut字符:

I have an .URL file which contains the following text which contains a German Umlaut character:

[Internet快捷方式]
URL = http://edn.embarcadero.com/article/44358
[MyApp]
Notes =特殊测试专家
图标=默认
Title = RAD Studio XE8的错误修复列表

[InternetShortcut]
URL=http://edn.embarcadero.com/article/44358
[MyApp]
Notes=Special Test geändert
Icon=default
Title=Bug fix list for RAD Studio XE8

我尝试使用TMemIniFile加载文本:

uses System.IniFiles;
//
procedure TForm1.Button1Click(Sender: TObject);
var
  BookmarkIni: TMemIniFile;
begin
  // The error occurs here:      
  BookmarkIni := TMemIniFile.Create('F:\Bug fix list for RAD Studio XE8.url',
                                    TEncoding.UTF8);
  try
    // Some code here
  finally
    BookmarkIni.Free;
  end;
end;

这是调试器中的错误消息文本:

This is the error message text from the debugger:

项目MyApp.exe引发带有消息的异常类EEncodingError '目标多字节中不存在Unicode字符的映射 代码页".

Project MyApp.exe raised exception class EEncodingError with message 'No mapping for the Unicode character exists in the target multi-byte code page'.

当我从.URL文件中删除带有德国Umlaut字符geändert"的单词时,就没有错误.

When I remove the word with the German Umlaut character "geändert" from the .URL file then there is NO error.

但这就是为什么我使用TMemIniFile的原因,因为当.URL文件中的文本包含Unicode字符时,TIniFile在这里不起作用. (.URL文件中也可能还有其他Unicode字符.)

But that's why I use TMemIniFile, because TIniFile does not work here when the text in the .URL file contains Unicode characters. (There could also be other Unicode characters in the .URL file).

那么为什么我在TMemIniFile.Create中出现异常?

So why I get an exception here in TMemIniFile.Create?

编辑:找到了罪魁祸首:.URL文件为ANSI格式. .URL文件为UTF-8格式时,不会发生此错误.但是,如果文件为ANSI格式怎么办?

EDIT: Found the culprit: The .URL file is in ANSI format. The error does not happen when the .URL file is in UTF-8 format. But what can I do when the file is in ANSI format?

EDIT2 :我创建了一种解决方法,该方法同时适用于ANSIUTF-8文件:

EDIT2: I've created a workaround which does work BOTH with ANSI and UTF-8 files:

procedure TForm1.Button1Click(Sender: TObject);
var
  BookmarkIni: TMemIniFile;
  BookmarkIni_: TIniFile;
  ThisFileIsAnsi: Boolean;
begin
  try
    ThisFileIsAnsi := False;
    BookmarkIni := TMemIniFile.Create('F:\Bug fix list for RAD Studio XE8.url',
                                    TEncoding.UTF8);
  except
    BookmarkIni_ := TIniFile.Create('F:\Bug fix list for RAD Studio XE8.url');
    ThisFileIsAnsi := True;
  end;
  try
    // Some code here
  finally
    if ThisFileIsAnsi then
      BookmarkIni_.Free
    else
      BookmarkIni.Free;
  end;
end;

您怎么看?

推荐答案

通常,无法从文件内容中自动检测文件的编码.

It is not possible, in general, to auto-detect the encoding of a file from its contents.

雷蒙德·陈(Raymond Chen)的这篇文章对此进行了清晰的演示:记事本文件编码问题,redux . Raymond以包含以下两个字节的文件为例:

A clear demonstration of this is given by this article from Raymond Chen: The Notepad file encoding problem, redux. Raymond uses the example of a file containing these two bytes:


D0 AE

Raymond继续显示这是一个格式正确的文件,具有以下四种编码:ANSI 1252,UTF-8,UTF-16BE和UTF-16LE.

Raymond goes on to show that this is a well formed file with the following four encodings: ANSI 1252, UTF-8, UTF-16BE and UTF-16LE.

这里带回家的教训是,您必须知道文件的编码.按照约定,无论谁与谁写入文件,都应同意.或强制存在BOM.

The take home lesson here is that you have to know the encoding of your file. Either agree it by convention with whoever writes the file. Or enforce the presence of a BOM.

这篇关于TMemIniFile.Create中的德语Umlaut字符异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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