【问题标题】:wcout does not output as desiredwcout 没有按需要输出
【发布时间】:2018-04-26 23:52:27
【问题描述】:

我一直在尝试为一个项目编写 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";
    }
}

输出是“类:Bili”,没有别的。程序甚至不尝试输出其他条目而只是挂起。我在使用 G++ 6.3.0 的 Windows 上。而且我没有使用 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++ 文件.cc -O3

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

【问题讨论】:

  • 您是否记得以 UTF-8 或 UTF-16 等 Unicode 格式保存您的源代码文件,特别是具有 Unicode 字符串文字的源代码文件?
  • 是的。我正在使用 VSCode,它的默认编码为 UTF8,刚刚检查过
  • 如果问题在于字符串的输出,为什么所有这些代码都带有向量和类来显示问题?只需 1 行 main 函数,只需 std::wcout &lt;&lt; L"Your string";
  • 因为'main函数'有效
  • 如何在一个函数 (main) 中输出您遇到问题的字符串,然后在输出相同字符时在另一个函数中失败?听起来你有一个错误,而不是一般输出字符串的问题。发布minimal reproducible example,因为发布所有这些代码而不是简单的 1 或 2 行程序会让人怀疑错误与编码无关。

标签: c++ windows utf-8 iostream widestring


【解决方案1】:

以下代码适用于我,在 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 facet see example here,并手动编写 BOM。


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

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

当然,您也可以使用窄流编写 UTF-16 文件,但不能使用来自 wstringoperator&lt;&lt;

【讨论】:

  • cppreference.com 说 codecvt_utf8 自 C++17 以来已被弃用,但它没有说明要使用什么,所以我现在坚持使用它......
  • 没关系。出于某种原因,您的答案不起作用,我设法通过使用 setlocale 函数来修复它。参考:Here。显然灌输wcout 是不够的。
  • codecvt_utf8 确实已被弃用,应使用 codecvt_utf8_utf16 代替。我会用它来更新答案。
  • 仅供参考,我使用较新的软件发布了答案
【解决方案2】:

如果您至少有 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;
   }
}

【讨论】:

    猜你喜欢
    • 2017-11-06
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 2015-12-02
    • 2022-01-01
    • 1970-01-01
    • 2012-05-09
    • 2017-07-13
    相关资源
    最近更新 更多