将字符串流传递到 unicode 项目中的控制台输出 [英] Passing a stringstream to console output in unicode project

查看:21
本文介绍了将字符串流传递到 unicode 项目中的控制台输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 MSVC++ 中,如果您创建一个新的 Visual Studio 控制台应用程序(x64 平台,在 Windows 8.1、x64 上运行),并在 main 中使用以下代码将其设置为 Unicode 字符集:

In MSVC++, if you create a new Visual Studio console application (x64 platform, running on Windows 8.1, x64), and set it to a Unicode character set with the following code in main:

int _tmain(int argc, _TCHAR* argv[])
{
    stringstream stream;
    stream << _T("Testing Unicode. English - Ελληνικά - Español.") << std::endl;
    string str = stream.str();
    std::wcout << str.c_str();
    cin.get();
}

它输出这个:

00007FF616443E50

00007FF616443E50

我希望它输出这个:

测试 Unicode.英语 - Ελληνικά - 西班牙语.

Testing Unicode. English - Ελληνικά - Español.

如何实现?

编辑:用 wstringstream 和 wstring 代替:

Edit: With wstringstream and wstring instead:

wstringstream stream; stream << _T("Testing Unicode. English - Ελληνικά - Español.") << std::endl;
wstring str = stream.str();
std::wcout << str.c_str();

输出被截断:

测试 Unicode.英语 -

Testing Unicode. English -

像这样设置模式:_setmode(_fileno(stdout), _O_U16TEXT);

输出仍然不理想,因为并非所有字符都能正确渲染:

The output is still undesirable because not all characters get rendered properly:

测试 Unicode.英语 - ????????- 西班牙语.

Testing Unicode. English - ???????? - Español.

像这样设置输出CP:SetConsoleOutputCP(CP_UTF8);

输出再次被截断:

测试 Unicode.英语 -

Testing Unicode. English -

推荐答案

单独使用以下方法是行不通的.您还必须做的是右键单击弹出的 Visual Studio 控制台.单击默认属性.单击字体选项卡并将字体设置为 Lucida Consolas.然后下面的代码将运行得很好.没有 << 的重载运算符 对于Windows,它将不起作用.您可能还想为 charwchar_t 进行重载,或者只是将其作为模板重载..

Using the following just doesn't work alone.. What you must also do is right click the Visual Studio console that pops up. Click Default Properties. Click the Fonts tab and set the font to Lucida Consolas. Then the below code will run just fine. Without the overloads of the << operator for windows, it will NOT work. You may also want to make an overload for char or wchar_t or simply make this a template overload..

如果你不喜欢重载,你可以使用 _setmode(_fileno(stdout), _O_U16TEXT);_setmode(_fileno(stdout), _O_U8TEXT);UTF16 和 UTF8 分别.

If you do not like the overloads, you may use _setmode(_fileno(stdout), _O_U16TEXT); or _setmode(_fileno(stdout), _O_U8TEXT); for UTF16 and UTF8 respectfully.

// Unicode.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <sstream>
#include <iostream>

#if defined _WIN32 || defined _WIN64
    #include <Windows.h>
#else
    #include <io.h>
    #include <fcntl.h>
#endif

#if defined _WIN32 || defined _WIN64
std::ostream& operator << (std::ostream& os, const char* data)
{
    SetConsoleOutputCP(CP_UTF8);
    DWORD slen = strlen(data);
    WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), data, slen, &slen, nullptr);
    return os;
}

std::ostream& operator << (std::ostream& os, const std::string& data)
{
    SetConsoleOutputCP(CP_UTF8);
    WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), data.c_str(), data.size(), nullptr, nullptr);
    return os;
}

std::wostream& operator <<(std::wostream& os, const wchar_t* data)
{
    DWORD slen = wcslen(data);
    WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), data, slen, &slen, nullptr);
    return os;
}

std::wostream& operator <<(std::wostream& os, const std::wstring& data)
{
    WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), data.c_str(), data.size(), nullptr, nullptr);
    return os;
}
#endif

int _tmain(int argc, _TCHAR* argv[])
{
    std::wstringstream stream;
    stream << _T("Testing Unicode. English - Ελληνικά - Español.") << std::endl;

    #if !defined _WIN32 && !defined _WIN64
        _setmode(_fileno(stdout), _O_U16TEXT);
    #endif

    std::wstring str = stream.str();
    std::wcout << str;
    std::wcin.get();
    return 0;
}

在 Windows 上,还有一件事可以帮助渲染任何语言的字体.. 我发现这没有在网上的其他任何地方发布.. 我导航到 Control PanelAppearance and PersonalizationFonts.我点击了Font Settings,然后取消选中Hide font based on language settings.保存了选项.这将允许您编写日语和中文字符以及阿拉伯语和您想要的任何其他语言.似乎也可以使用默认的控制台字体.不过我必须重新启动才能生效.不确定它是否真的适用于其他任何人..

On Windows there is ONE more thing that can help render fonts in ANY language.. I found this was not posted anywhere else on the net.. I navigated to Control PanelAppearance and PersonalizationFonts. I clicked Font Settings and then unchecked Hide fonts based on language settings. Saved the options. This will allow you to write Japanese and Chinese characters as well as arabic and whatever other languages you want. Seems to work with the default console fonts as well.. I had to restart for it to take effect though. Not sure if it actually works for anyone else..

这篇关于将字符串流传递到 unicode 项目中的控制台输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆