在ini文件中存储unicode字符串时,可能会出现与编码相关的问题? [英] Could there be encoding-related problems when storing unicode strings in ini files?

查看:350
本文介绍了在ini文件中存储unicode字符串时,可能会出现与编码相关的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经有关于unicode和ini文件的问题,但是其中许多是特定于域的.因此,我不确定答案是否可以适用于一般情况.

There are already questions regarding unicode and ini files, but many of them are rather domain-specific. So I am not sure if the answer can be applied to the general case.

动机:我想使用ini文件来存储简单的数据,例如一些数字和一些字符串.字符串由用户提供(通过GUI输入).该软件可以在世界任何地方运行,可以使用任何语言.这些文件还可以在用户之间共享(因此可以在一个系统上写入文件,在另一个系统上读取文件,依此类推).

Motivation: I want to use ini files for storing simple data like some numbers and some strings. The strings are provided by users (input via GUI). The software could run anywhere in the world, any language can be used. The files also can be shared between users (so they can be written to on one system, read on another and so on).

我认为使用GetPrivateProfileStringWWritePrivateProfileStringW(我的目标系统是Windows XP)时,ini文件中的unicode应该没问题.

I figured that unicode in ini files should be no problem when using GetPrivateProfileStringW and WritePrivateProfileStringW (I am targeting systems >= Windows XP).

但是后来我偶然发现了这个问题.

But then I stumbled upon an answer in this question.

报价:

WritePrivateProfileStringW函数将以旧版系统编码(例如日语系统上的Shift-JIS)写入INI文件,因为它是旧版支持功能.如果要具有完全启用Unicode的INI文件,则需要使用外部库.

The WritePrivateProfileStringW function will write the INI file in legacy system encoding (e.g. Shift-JIS on a Japanese system) because it is a legacy support function. If you want to have a fully Unicode-enabled INI file, you will need to use an external library.

我现在不确定-我需要担心吗?还是我可以继续使用ini文件?

I am unsure now - do I have to worry? Or can I just go ahead and use ini files?

看来避免随机编码的关键可能是准备一个包含BOM表的空文件,然后使用该文件.有没有人(正面/负面)经历过?

It seems the key to avoid random encodings might be to prepare an empty file containing a BOM, then using this file. Has anyone (positive/negative) experience with this?

推荐答案

答案是:是的,根据文件是否已经存在以及(如果存在)文件的内容编码方式,可能会出现问题.

The answer is: Yes, there can be problems depending on whether the file already exists and (if it exists) how its content is encoded.

如果ini文件的内容已经是Unicode,则将其视为Unicode.在内部,这似乎由 IsTextUnicode 函数.对于此功能,文件中正确的BOM是Unicode的重要提示.因此,仅使用WritePrivateProfileStringW不能确保将Unicode写入ini文件,而是必须准备文件.

An ini file is treated as Unicode if its content is already Unicode. Internally this seems to be determined by the IsTextUnicode function. And for this function the right BOM in the file serves as a big hint towards Unicode. So just by using WritePrivateProfileStringW you cannot ensure to write Unicode to the ini file, instead you have to prepare the file.

来源:迈克尔·卡普兰的博客

这篇关于在ini文件中存储unicode字符串时,可能会出现与编码相关的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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