3

I was trying to properly handle unicode input/output in winconsole (Polish Windows 10) using std::wcin, std::wcout, std::wstring, etc. I am using Visual Studio 2015 and Character Set in Property Pages is set to Unicode. After some search and couple of tries I managed to get it work with:

_setmode(_fileno(stdout), _O_U16TEXT);
_setmode(_fileno(stdin), _O_U16TEXT);
std::locale::global(std::locale(""));

The problem is I do not quite understand why it works and why I have to use both _setmode and std::locale. When I remove _setmodes output is fine, but non-ascii chars on input aren't handled properly. Commenting std::locale line makes the program stop working on the very first appearance of non-ascii char on output.

Does someone use the same thing? Could you explain briefly what those functions do? I want to undestand what exactly I am doing.

Dima Chubarov
  • 16,199
  • 6
  • 40
  • 76
PKua
  • 463
  • 3
  • 15
  • What is it you"re trying to achieve. Any reason you can't use utf-8? If you need multilingual handling, this is what you should use – Chris Halcrow Nov 14 '16 at 19:54
  • @ChrisHalcrow Windows uses UTF-16 internally, so using it in your program is certainly defensible. This is a perfectly valid question as it stands. – Mark Ransom Nov 14 '16 at 20:15
  • The thing is it's working and to be honest I do not know how. What would you guys suggest to do? – PKua Nov 14 '16 at 20:26
  • 1
    There are lots of questions here about getting Unicode working in Windows. The problem is that there is no standard way to tell the runtime library whether Unicode will be UTF 32, UTF-16, UTF -8, or something else. Here are some sample questions: https://stackoverflow.com/questions/2492077/output-unicode-strings-in-windows-console-app https://stackoverflow.com/questions/24307072/how-can-i-make-dynamic-strings-to-work-with-utf-8-in-console https://stackoverflow.com/questions/10900106/how-to-send-international-characters-to-windows-console – Jerry Jeremiah Nov 29 '20 at 20:26

0 Answers0