How to output unicode in C++ without _setmode - c++

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"

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.

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>

Related

What header do I have to include to write Hangeul out to a console window using Visual Studio 2022?

First, briefly,
My computer with Window11OS showed this console window.
?? ? ?? C++????
C:\Users\MYNAME\source\repos\20220816ConsoleApplication1\x64\Debug\20220816ConsoleApplication1.exe (process 17604) exited with code 0.
and quetionmarks are NOT what I intended.
I think I need to use intentionally UNICODE here.
But I don't know how to do, I failed once; I'll explain later.
So, this is the code that I want to fix. And I'm using Visual Studio as Windows Console Application(C++/WInRT) for this solution-file.
#include <iostream>
int main()
{
using namespace std;
/*표준 출력 스트림으로 문장을 출력함
근데 왜 한글이 물음표로 나올까*/
cout << "나의 첫 번째 C++프로그램" << endl;
return 0;
}
But VS's error-list showed me the below.
Warning C4566 character represented by universal-character-name '\uB098' cannot be represented in the current code page (1252)
What property I've changed to build successfully was the below
VS's upper menu > Project > Properties(the lowest item) > Configuration Properties > C/C++ > Precompiled Headers > Precompiled Header : Use(/Yu) --I CHANGED IT TO-->> Not Using Precompiled Headers
I also had tried to change "Language" property just like the screenshot below, but I had failed to build at all, even question-marks hadn't shown.
What I had tried to show Hangeul-language instead of question-marks but failed.
On Windows, use the following to set the console stdout to Unicode mode. Save the file with UTF-8 w/ BOM encoding, or UTF-8 without BOM and compile with the /utf-8 switch. You Windows needs to support the Korean language or at least use a console font that supports Korean characters such as NSimSun.
test.cpp: compiled with "cl /W4 /EHsc /utf-8 test.cpp"
#include <iostream>
#include <io.h>
#include <fcntl.h>
int main()
{
using namespace std;
_setmode(_fileno(stdout), _O_U16TEXT);
wcout << L"나의 첫 번째 C++프로그램" << endl;
return 0;
}
Output (cut/paste from cmd window):
나의 첫 번째 C++프로그램

How to print 4 byte Unicode character in Windows C++ console app?

How to print "👩" emoji (Unicode code 1F469) in Windows console app using C++?
In example below I followed Printing UTF-8 Text to the Windows Console.
#include <iostream>
#include <io.h>
#include <fcntl.h>
int main()
{
_setmode(_fileno(stdout), _O_U16TEXT);
std::wcout << L"face: 👩\n";
return 0;
}
However it only prints two questionmarks:
.
"Command Prompt" (cmd.exe) app can't render this char so I'm using Windows Terminal that can render it:
The Windows Console cannot display characters outside of Plane 0. The Windows Terminal was designed to improve on the limitations of the Windows Console.
Further reading: How to use unicode characters in Windows command line?

Unicode output on windows console

The article Unicode apps in the MinGW-w64 wiki explains the following example for an Unicode application, e.g. _main.c_:
#define UNICODE
#define _UNICODE
#include <tchar.h>
int _tmain(int argc, TCHAR * argv[])
{
_tprintf(argv[1]);
return 0;
}
The above code makes use of tchar.h mapping, which allows it to both compile in Unicode and non-Unicode mode. [...] The -municode option is still required when linking if Unicode mode is used.
So I used
C:\> i686-w64-mingw32-gcc main.c -municode -o hello
_tmain.c:1:0: warning: "UNICODE" redefined
#define UNICODE
^
<command-line>:0:0: note: this is the location of the previous definition
to compile a Unicode application. But, when I run it, it returns
C:\> hello Süßer
S³▀er
So the Unicode string is wrong. I used the latest version 4.9.2 of MinGW-w64, i686 architecture and tried the Win32 and POSIX theads variants, both result in the same error. My operating system is 32-bit German Windows 7. When I used the Unicode codepage (chcp 65001), I have to use the font "Lucida Console". With this setting I get a similar error:
C:\> hello Süßer
S��er
I want to use a parameter with "ü" or "ß" in a Windows C++ program.
Solution
nwellnhof is right: The problem is the output on the console. This problem is explained in Unicode part 1: Windows console i/o approaches und Unicode part 2: UTF-8 stream mode. The latter gives a solution for Visual C++ - it worked also with Intel C++ 15. This blog post does "not yet consider the g++ compiler. All this code is Visual C++ specific. However, [the blog author has] done generally the same with g++, and [he] will probably discuss that in a third installment."
I want to open a file, which name is given by a parameter. This works simple, e. g. main.c:
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[])
{
if ( argc > 1 ) {
// The output will be wrong, ...
cout << argv[1] << endl;
// but the name of this file will be right:
fstream fl_rsl(argv[1], ios::out);
fl_rsl.close();
}
return 0;
}
and the compilation without unicode mode
C:\> g++ main.cpp -o hello && hello Süßer
It's console output is still wrong, but the created filename is right. This is okay for me.

Required file "tracker.exe" is missing

First time using Visual C++ in Visual Studio and I'm trying to teach myself C++ from some books. I am just trying to do a basic "Hello World" program and its getting a couple errors that I don't know anything about, as well as a bizarre warning. The missing source file seems to be standard and I can't figure it out.
The errors:
Error 2 error : Required file "tracker.exe" is missing.
Error 3 IntelliSense: cannot open source file "SDKDDKVer.h"
The warning:
warning MSB8003: Could not find WindowsSDKDir variable from the registry. TargetFrameworkVersion or PlatformToolset may be set to an invalid version number.
The code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" ;
// This prevents the Console Window from closing during debug mode
cin.ignore(cin.rdbuf()->in_avail());
cout << "\nPress only the 'Enter' key to exit program: ";
cin.get();
return 0;
}
Any explanations or help would be huge! Thanks.
Here's a standard C++03 "hello, world!":
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!" << endl;
}
Turn off precompiled headers in the project settings. Make that compile without adding anything. Run it via Ctrl F5 (alternatively place a breakpoint on the final } and run it via the debugger, e.g. keypress F5).
If that doesn't work then you have configuration error. Then try first a new project. If that doesn't work, uninstall VS and reinstall it.

_O_WTEXT, _O_U16TEXT, _O_U8TEXT - are these modes possible in mingw compiler, are there any workarounds?

#include <fcntl.h>
#include <io.h>
#include <stdio.h>
int main(void) {
_setmode(_fileno(stdout), _O_U16TEXT);
wprintf(L"\x043a\x043e\x0448\x043a\x0430 \x65e5\x672c\x56fd\n");
return 0;
}
returns error at compilation: _O_U16TEXT was not declared in this scope
Is this a show-stopper with this compiler ?
Well, there's a simple workaround: just use values of these constants instead of their names. For example, _O_U16TEXT is 0x00020000 and _O_U8TEXT is 0x00040000.
I've just confirmed that it works with _setmode using g++ 4.8.1 on Windows 10:
#include <iostream>
#include <fcntl.h>
#include <io.h>
#include <stdio.h>
int main() {
_setmode(_fileno(stdout), 0x00020000); // _O_U16TEXT
std::wcout << L"Русский текст\n";
}
Don't even bother with it.
Your user needs to set the font of his console to a unicode one (Lucida Console for example), or else it won't display anything. If you're targeting East Asia, where the default font is unicode aware, it'll display fine, but in Europe, you have to change the font manually.
User's won't change their console font because you write it in the readme. They won't even read the readme, they'll just see that it displays garbage, and they will think the program doesn't work.
Also, it's a farly new function VS 2005, so you need to link the VS 2005 redistributeable, msvcr50.dll, or newer (mingw link to msvcrt.dll by default). If you don't do this, the text, won't be displayed at all.
And I think a simple pipe will kill your new and shiny unicode text. Let's say your exe named a.exe. I'm pretty sure, that a.exe | more will NOT display unicode text.