How to output unicode in C++ without _setmode
Asked Answered
L

2

6

I'm trying to insert a unicode value, in this case, \u250F, or ┏, to the console output. I have searched around, and people recommending a variety of things. Before we discuss on what I tried, I am using Windows and Visual Studio 2013.

The main error

When I try multiple 'fixes', if not specified, I always get the same error:

Debug Assertion Failed!
Program: ...nts\visual studio 2013\Projects\roguelike\Debug\roguelike.exe
File: f:\dd\vctools\crt\crtw32\stdio\fputc.c
Line: 48

Expression: ((_Stream->_flag & _IOSTRG) || ( fn = _fileno(_Stream), 
((_textmode_safe(fn) ==  _IOINFO_TM_ANSI && !_tm_unicode_safe(fn))))

For more information on how your program can cause an assertion failure, 
see the Visual C++ documentation on asserts

(Press Retry to debug the application)

What I have tried

I have tried to do all of the following things to output it:

std::cout << "\u250F";
std::wcout << "\u250F";
std::cout << L"\u250F";
std::wcout << L"\u250F";
std::cout << "┏";
std::wcout << "┏";
std::cout << L"┏";
std::wcout << L"┏";

How I tried to tell the console how to output unicode

_setmode(_fileno(stdout), _O_U16TEXT);

Seems to be the problem, really.

system("chcp 65001");

Doesn't error, but doesn't do anything

std::locale::global(std::locale("en_US.utf8"));

It causes an error of where the parameters are "illegal"

Ligature answered 10/11, 2014 at 0:19 Comment(2)
fflush(stdout); _setmode(_fileno(stdout), _O_U16TEXT); std::wcout << L"\u250f"; works for me with VC++ 10.Tacet
You can also call the Windows API directly, e.g. WriteConsoleW.Tacet
R
1

Following code works for me: my ENV: vs2013, win7_x64, system locale is 'English (United States)'

SetConsoleOutputCP(CP_UTF8);
_setmode(_fileno(stdout), _O_U8TEXT);
wprintf(L"Interface %s\r\n", pNP->pszwName);   //pszwName is a wchar_t*

Another tricks is your console font, does your console font include char character '┏ '?

google 'cmd font fontlink' to add a font to your cmd console.

run 'chcp 65001' in your cmd console before execute your program.

Roughhouse answered 12/1, 2015 at 3:12 Comment(2)
Calling both SetConsoleOutputCP and _setmode is counterproductive.Tremble
@Tremble Why?Refulgent
A
0

Had the same issue. Solved it by switching the order of my pre-processor commands.

If you have included io.h and fcntl.h this way:

#include <fcntl.h>  
#include <io.h>

switch them so that they're this way:

#include <io.h>
#include <fcntl.h>
Andorra answered 6/2, 2017 at 0:35 Comment(1)
Tried to invert but still get the same assertion errorGodforsaken

© 2022 - 2024 — McMap. All rights reserved.