在C ++中使用UTF-8 std :: string对象

恶霸威利广场

我使用Visual Studio,并C++Windows与小型大写文字工作像ʜᴇʟʟᴏ ꜱᴛᴀᴄᴋᴏᴠᴇʀꜰʟᴏᴡ例如使用这个网站。每当我从文件中读取此文本或使用将该文本直接放入源代码时std::string,文本可视化工具Visual Studio都会以错误的编码方式显示它,大概是可视化工具使用的Windows (ANSI)如何强迫Visual StudioUTF-8正确使用琴弦?

std::string message_or_file_path = "...";
auto message = message_or_file_path;

// If the file path is valid, read from that file
if (GetFileAttributes(message_or_file_path.c_str()) != INVALID_FILE_ATTRIBUTES
    && GetLastError() != ERROR_FILE_NOT_FOUND)
{
    std::ifstream file_stream(message_or_file_path);
    std::string text_file_contents((std::istreambuf_iterator<char>(file_stream)),
        std::istreambuf_iterator<char>());
    message = text_file_contents; // Displayed in wrong encoding
    message = "ʜᴇʟʟᴏ ꜱᴛᴀᴄᴋᴏᴠᴇʀꜰʟᴏᴡ"; // Displayed in wrong encoding
   std::wstring wide_message = L"ʜᴇʟʟᴏ ꜱᴛᴀᴄᴋᴏᴠᴇʀꜰʟᴏᴡ"; // Displayed in correct encoding
}

我尝试了其他命令行选项/utf-8来编译和设置区域设置:

std::locale::global(std::locale(""));
std::cout.imbue(std::locale());

这些都不能解决编码问题。

恶霸威利广场

一个可行的解决方案就是简单地将所有std::strings重写std::wstrings并适当地调整代码逻辑以使其与std::wstrings一起使用,如问题中所示。现在一切正常。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在C ++中的UTF-8上正确使用std :: string?

在C ++ 17中使用const std :: string&参数是否有意义?

具有u8,char8_t和std :: string的C ++ 20

使用C样式字符串文字与构造未命名std :: string对象的默认建议?

C ++使用std :: sscanf和std :: string

std :: string在安全的地方截断utf-8的最佳方法

从utf8字符将jstring(JNI)转换为std :: string(c ++)

XmlWriter在C#中使用StringWriter编码UTF-8

在C ++ 11中缺少std :: u8string

在std :: string中使用非法的UTF-8八位字节作为分隔符

在C ++中将utf-8文件读取到std :: string

使用C函数操作std :: string

如何将UTF8 std :: string转换为NSString?

将std :: u8string复制到utf8字符的c样式字符串中

使用g ++ 8和c ++ 20的std :: async的编译问题

如何在Windows中使用C ++输出和输入UTF8或UTF16 Unicode文本?

不能在UTF-8中使用`cut -c`(`--characters`)吗?

std :: string和UTF-8编码的unicode

在C ++中使用utf8库将UTF 16转换为UTF8

C ++在std :: map <>中使用std :: set <>

如何在C ++中使用对象序列化保存和加载std :: string?

C ++正确使用std :: set与对象

何时在python中使用unicode(string)和string.encode('utf-8')

如何在NodeJs本机插件中使用非包装C ++对象的std :: map

在url,codeigniter,seo中使用utf 8

Libcurl C ++将std :: string转换为cv :: Mat对象

在C ++ 11中使用auto foo =“ bar” vs std :: string

使用 std::views 将 std::vector<string> 转换为 C++20 中的 std::string

如何將 std::string 轉換為 UTF-8?