wcout 没有按要求输出

wcout does not output as desired

我一直在尝试为项目编写 C++ 应用程序,我 运行 遇到了这个问题。基本上:

class OBSClass
{
public:
    wstring ClassName;
    uint8_t Credit;
    uint8_t Level;
    
    OBSClass() : ClassName(), Credit(), Level() {}
    OBSClass(wstring name, uint8_t credit, uint8_t hyear)
    : ClassName(name), Credit(credit), Level(hyear)
    {}
};

在其他文件中:

vector<OBSClass> AllClasses;
...
AllClasses.push_back(OBSClass(L"Bilişim Sistemleri Mühendisliğine Giriş", 3, 1));
AllClasses.push_back(OBSClass(L"İş Sağlığı ve Güvenliği", 3, 1));
AllClasses.push_back(OBSClass(L"Türk Dili 1", 2, 1));
... (rest omitted, some of entries have non-ASCII characters like 'ş' and 'İ')

我有一个函数基本上输出 AllClasses 中的所有内容,问题是 wcout 没有按预期输出。

void PrintClasses()
{
    for (size_t i = 0; i < AllClasses.size(); i++)
    {
        wcout << "Class: " << AllClasses[i].ClassName << "\n";
    }
}

输出为 'Class: Bili',仅此而已。程序甚至不尝试输出其他条目而只是挂起。我正在 windows 使用 G++ 6.3.0。而且我没有使用 Windows' cmd,我使用的是来自 mingw 的 bash,所以编码不会有问题(或者不是吗?)。有什么建议吗?

编辑:源代码编码也没有问题,刚刚检查过它是UTF8,默认为VSCode

编辑:还检查了问题是否出在字符串文字上。

wstring test;
wcin >> test;
wcout << test;

输入了一些非 ASCII 字符,例如“ö”和“ş”,它运行良好。宽字符串文字有什么问题?

编辑:给你

#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<wstring> testvec;

int main()
{
    testvec.push_back(L"Bilişim Sistemleri Mühendisliğine Giriş");
    testvec.push_back(L"ıiÖöUuÜü");
    testvec.push_back(L"☺☻♥♦♣♠•◘○");
    for (size_t i = 0; i < testvec.size(); i++)
        wcout << testvec[i] << "\n";
    return 0;
}

使用 G++ 编译: g++ file.cc -O3

这段代码只输出'Bili'。它一定是 g++ 搞砸了二进制编码(?),因为用 wcin 输入值然后用 wcout 输出它们不会产生任何问题。

以下代码适用于我,在 MSYS2 Bash 和 Windows CMD 中使用 MinGW-w64 7.3.0;并将源代码编码为 UTF-8:

#include <iostream>
#include <locale>
#include <string>
#include <codecvt>

int main()
{
    std::ios_base::sync_with_stdio(false);

    std::locale utf8( std::locale(), new std::codecvt_utf8_utf16<wchar_t> );
    std::wcout.imbue(utf8);

    std::wstring w(L"Bilişim Sistemleri Mühendisliğine Giriş");
    std::wcout << w << '\n';
}

解释:

  • Windows 控制台不支持任何类型的 16 位输出;它只是 ANSI 和部分 UTF-8 支持。所以你需要配置 wcout 将输出转换为 UTF-8。这是向后兼容目的的默认设置,尽管 Windows 10 1803 确实添加了一个选项以将其设置为 UTF-8 (ref).
  • imbuecodecvt_utf8_utf16 实现了这一点;但是您还需要禁用 sync_with_stdio 否则流甚至不使用该方面,它只是遵从具有类似问题的 stdout

对于写入其他文件,我发现同样的技术也适用于写入 UTF-8。要编写 UTF-16 文件,您需要将 wofstream 与 UTF-16 方面 see example here 结合,然后手动编写 BOM。


评论:由于这些问题,许多人只是避免尝试完全使用宽 iostream。

您可以使用窄流写入 UTF-8 文件;并在代码中调用函数将 wstring 转换为 UTF-8,如果您在内部使用 wstring;您当然可以在内部使用 UTF-8。

当然,您也可以使用窄流编写 UTF-16 文件,只是不能使用来自 wstring.

operator<<

如果您至少 Windows 10 1903(2019 年 5 月),并且至少 Windows 终端 0.3.2142(2019 年 8 月)。然后设置Unicode:

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage]
"OEMCP"="65001"

然后重启。之后你可以使用这个:

#include <iostream>

int main() {
   std::string a[] = {
      "Bilişim Sistemleri Mühendisliğine Giriş",
      "Türk Dili 1",
      "İş Sağlığı ve Güvenliği",
      "ıiÖöUuÜü",
      "☺☻♥♦♣♠•◘○"
   };

   for (auto s: a) {
      std::cout << s << std::endl;
   }
}