Could there be encoding-related problems when storing unicode strings in ini files?

后端 未结 2 865
我寻月下人不归
我寻月下人不归 2021-01-25 16:58

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.

相关标签:
2条回答
  • 2021-01-25 17:52

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

    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.

    Source: Michael Kaplan's Blog

    0 讨论(0)
  • 2021-01-25 17:56

    The problem is not really with the use of ini files but with the functions you'll use to read from and write to those files.

    As you noticed, WritePrivateProfileStringW() will not write UNICODE data to the file. Instead, it will use whatever multi-byte encoding is standard on the system. That means that ini files created on a Japanese system won't be readable on a Russian system. The reverse is also true.

    If the files are not intended to be shared by systems with different encodings, you'll be fine. Otherwise, maybe you shouldn't use ini files but a more UNICODE-aware technology, like e.g. XML, whose encoding defaults to UTF-8 on all platforms.

    0 讨论(0)
提交回复
热议问题