C++ / wcout / UTF-8

C++ / wcout / UTF-8

本文关键字:UTF-8 wcout C++      更新时间:2023-10-16

我正在读取一个 UTF-8 编码的 unicode 文本文件,并将其输出到控制台中,但显示的字符与我用于创建文件的文本编辑器中的字符不同。这是我的代码:

#define UNICODE
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include "pugixml.hpp"
using std::ifstream;
using std::ios;
using std::string;
using std::wstring;
int main( int argc, char * argv[] )
{
    ifstream oFile;
    try
    {
        string sContent;
        oFile.open ( "../config-sample.xml", ios::in );
        if( oFile.is_open() )
        {
            wchar_t wsBuffer[128];
            while( oFile.good() )
            {
                oFile >> sContent;
                mbstowcs( wsBuffer, sContent.c_str(), sizeof( wsBuffer ) );
              //wprintf( wsBuffer );// Same result as wcout.
                wcout << wsBuffer;
            }
            Sleep(100000);
        }
        else
        {
            throw L"Failed to open file";
        }
    }
    catch( const wchar_t * pwsMsg )
    {
        ::MessageBox( NULL, pwsMsg, L"Error", MB_OK | MB_TOPMOST | MB_SETFOREGROUND );
    }
    if( oFile.is_open() )
    {
        oFile.close();
    }
    return 0;
}

一定有一些我不明白

的编码。

问题是mbstowcs实际上并不使用 UTF-8。它使用旧式的"多字节代码点",与 UTF-8 不兼容(尽管从技术上讲是可能的 [我相信] 定义 UTF-8 代码页,但在 Windows 中没有这样的东西)。

如果要将 UTF-8 转换为 UTF-16,可以使用 MultiByteToWideCharcodepageCP_UTF8

宽字符串并不意味着 UTF-8。事实上,情况恰恰相反:UTF-8 表示 Unicode 转换格式(8 位);这是一种在 8 位字符上表示 Unicode 的方法,因此您的正常char s。您应该将其读入普通字符串(而不是宽字符串)。

宽字符串使用wchar_t,在Windows上是16位。操作系统使用 UTF-16 实现其"广泛"功能。

在 Windows 上,可以使用 MultiByteToWideChar 将 UTF-8 字符串转换为 UTF-16

我制作了一个C++ char_t容器,最多可容纳 6 个 8 位char_t将其存储在std::vector中。将其与wchar_t相互转换或追加到std::string

在这里查看:在 Github 上查看 UTF-8_String 结构

#include "UTF-8_String.h" //header from github link above
iBS::u8str  raw_v;
iBS::readu8file("TestUTF-8File.txt",raw_v);
std::cout<<raw_v.str()<<std::endl;

这是将wchar_t转换为上面标题中 u8char 结构中的uint32_t的函数。

    #include <cwchar>
    u8char& operator=(wchar_t& wc)
    {
        char temp[6];
        std::mbstate_t state ;
        int ret = std::wcrtomb((&temp[0]), wc, &state);
        ref.resize(ret);
        for (short i=0; i<ret; ++i) 
            ref[i]=temp[i];
        return *this;
    };

我发现wifstream工作得很好,即使在Visual Studio调试器中也能正确显示UTF-8单词(我正在阅读繁体中文单词),来自这篇文章:

#include <sstream>
#include <fstream>
#include <codecvt>
std::wstring readFile(const char* filename)
{
    std::wifstream wif(filename);
    wif.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t>));
    std::wstringstream wss;
    wss << wif.rdbuf();
    return wss.str();
}
 
//  usage
std::wstring wstr2;
wstr2 = readFile("C:\yourUtf8File.txt");
wcout << wstr2;