How to read/write Chinese/Japanese characters from/to INI files?

早过忘川 提交于 2019-11-30 19:31:50

问题


Using WritePrivateProfileString and GetPrivateProfileString results in ??? instead of the real characters.


回答1:


GetPrivateProfileString() and WritePrivateProfileString() will work with Unicode, sort of.

If the ini file is UTF-16LE encoded, i.e. it has a UTF-16 BOM, then the functions will work in Unicode. However if the functions have to create the file they will create an ANSI file and only work in ANSI.

So to use the functions with Unicode, create your ini file before you first use it and write a UTF-16LE Byte Order Mark to it. Then carry on as normal.

Note that the functions do not work at all with UTF-8.

See Michael Kaplan's blog for more detail than you ever wanted to know about this.




回答2:


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.

Try SimpleIni http://code.jellycan.com/simpleini/

It is C++, single header file, template library with an MIT licence (i.e. commercial use is OK). Include it into your source file and use it. It is cross-platform, supports UTF-8 and legacy encoded files, and can read and write the INI file largely preserving comments and structure, etc. Easiest to check out the page.

It's been around for a while and is appears to be used by quite a number of people. I wrote it and continue to support it.




回答3:


According to the WritePrivateProfileString documentation, there is a Unicode version: WritePrivateProfileStringW. Use that, and you should be able to use Unicode characters.




回答4:


It might just be a problem with how you are displaying or handling the strings. For example, the normal console window can't display japanese strings with printf.

Can you post some of your code?



来源:https://stackoverflow.com/questions/204241/how-to-read-write-chinese-japanese-characters-from-to-ini-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!