求助 读取utf8编码中文文件显示乱码问题

求助 读取utf8编码中文文件显示乱码问题


开发环境:

  • Windows: Windows 11 Pro 22H2

  • IDE: Clion 2023.1.3

  • Toolchain: Visual Studio

  • Generator: Visual Studio 17 2022

  • CMake: 3.26.3

  • Cocos版本: image


CMake 中指定了

add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")

开发环境选择的 UTF-8
image

JSON文件已使用Notepad++指定 UTF-8 编码 编辑器Label或者其他的中文都可以直接显示
image

唯独读取JSON文件时乱码 查看了编译后的Resource目录下的json文件 编码 UTF-8 Without BOM 也是正常的
JSON解析使用

读取代码:

// ###1
{
    std::string data = FileUtils::getInstance()->getStringFromFile(lanFile);
}

// ###2
{
    auto ful = FileUtils::getInstance()->fullPathForFilename(lanFile);
    ifstream stream(ful);
    if (!stream)
    {
        CCLOGWARN("Error Read");
    }
    else
    {
        std::string str_((std::istreambuf_iterator<char>(stream)),
                         std::istreambuf_iterator<char>());
        CCLOGINFO(str_.c_str());
    }
}

尝试了以上两种方法读取;最后结果相同

GDB中显示的字符串:

JSON解析使用 RAPIDJSON

能在游戏中正常显示不就行了?
有可能是控制台的原因,把控制台也设置为utf8?

不行,游戏中创建出来显示两个框框