c++ libconfig配置文件包含中文的问题

最近在做流程配置化,选取了libconfig开源库,感觉使用起来很灵活,api也比较友好,也有官方的文档(https://github.com/hyperrealm/libconfig)。

windows下安装直接打开对应sln编译即可,2017以下用2018.sln,以上就用2017.sln。用时需引用lib目录下的头文件。

但是配置文件出现中文时,libconfig读取会出现乱码,查找原因发现是我用的Unicode编码的配置文件,但libconfig是不支持Unicode的,官方的说法是:

Libconfig does not natively support Unicode configuration files, but string values may contain Unicode text encoded in UTF-8; such strings will be treated as ordinary 8-bit ASCII text by the library. It is the responsibility of the calling program to perform the necessary conversions to/from wide (wchar_t) strings using the wide string conversion functions such as mbsrtowcs() and wcsrtombs() or the iconv() function of the libiconv library.

需要自己转换,但我自己测试了一下,发现读取utf-8编码的中文时,libconfig拿到的字符串ASCII码和我自己在程序中生成的相同中文字符串的ASCII码不一样,如图。

 

 目前我不知道是为什么,之后我将配置文件转为GBK编码,是能正常读取到中文的。

posted @ 2021-07-14 11:00  秦道友  阅读(230)  评论(0编辑  收藏  举报