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

时间:2021-07-14
本文章向大家介绍c++ libconfig配置文件包含中文的问题,主要包括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编码,是能正常读取到中文的。

原文地址:https://www.cnblogs.com/qinhq16/p/15009883.html