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

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

问题描述

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

  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


我希望改为输出:


测试Unicode。英语 - Ελληνικά - Español。


如何实现?



编辑:使用wstringstream和wstring代替:

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

输出被截断:


测试Unicode。英语 -


设置模式如下: _setmode(_fileno(stdout),_O_U16TEXT);



输出仍然不受欢迎,因为并非所有字符都能正确呈现:


测试Unicode。英语 - ???????? - Español。


设置输出CP如下: SetConsoleOutputCP(CP_UTF8);



输出会被截断:


测试Unicode。英语 -



解决方案

使用以下内容不能单独使用。做是右键单击弹出的Visual Studio控制台。单击默认属性。单击字体选项卡并将字体设置为Lucida Consolas。然后下面的代码将运行就好了。没有<<操作符对于窗口,它将不工作。你可能还想对 char wchar_t 进行重载,或者只是使这个模板超载..



如果你不喜欢重载,可以使用 _setmode(_fileno(stdout),_O_U16TEXT); _setmode(_fileno(stdout),_O_U8TEXT);

$

 
$ b

  // Unicode.cpp:定义控制台应用程序的入口点。 
//

#includestdafx.h
#include< sstream>
#include< iostream>

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

#if定义_WIN32 ||定义_WIN64
std :: ostream&运算符<< (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&运算符<< (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& <<<<(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上还有一件事可以帮助渲染任何语言的字体。 。我发现这不是在网上的任何其他地方。我导航到控制面板\外观和个性化\字体。我点击了字体设置,然后取消选中隐藏基于语言设置的字体。保存选项。这将允许你写日语和汉字以及阿拉伯语和任何其他语言你想要的。似乎使用默认控制台字体以及..我不得不重新启动它为它生效。不知道它是否真的适用于任何人。


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();
}

It outputs this:

00007FF616443E50

I would like it to output this instead:

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

How can this be achieved?

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();

The output is truncated:

Testing Unicode. English -

Setting the mode like so: _setmode(_fileno(stdout), _O_U16TEXT);

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

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

Setting the output CP like so: SetConsoleOutputCP(CP_UTF8);

The output is again truncated:

Testing Unicode. English -

解决方案

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..

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;
}

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 Panel\Appearance and Personalization\Fonts. 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..

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

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