0

Everything that I want to do it's just only to output one line of text:

 #include <iostream>
 #include <string>

 int main(void)
 {
   std::wstring    d=L"string_contains_many_languages_German_Mädchenß_Russian_этотязык_and_ブリザンメント";
   std::wcout<<d<<"\n";

   return 0;
 }

I heard about locales. But what if I have a multilingual user? So,I can't stick to only one locale.

It works properly neither in Linux/Ubuntu with g++ compiler nor in Windows XP.

I suspect that perhaps it somehow depends not only from C++, but from terminal that shows this info and from environment. In Linux I can create a file with the name using command touch "ブリザаäЯ" (tilda terminal).

I wonder whether it's possible at least to create a file with the name that I have in a string like d (in code) using C++ means

Correction:

Also I want to do these operations on Windows and Linux

Tebe
  • 3,176
  • 8
  • 40
  • 60
  • 1
    On Linux, filenames are opaque byte sequences with no semantics (short of being passed as null-terminated strings in the API). On Windows, filenames are opaque sequences of 16-bit integers, generally assumed (but not required) to constitute UTF-16 code units. Perhaps [this little rant](http://stackoverflow.com/q/6300804/596781) of mine is useful, too. – Kerrek SB Sep 13 '12 at 21:21
  • 3
    This is not a locale issue. A locale incorporates aspects such as how decimals are represented in different countries. What you are referring to is Unicode versus ASCII. All you want to be able to do is write out Unicode characters, hence the `w` in `wstring`, meaning wide character representation. – Steztric Sep 13 '12 at 21:28
  • "It works properly neither in ". What happened when you tried? It should work properly in both of those environments. – Mooing Duck Sep 13 '12 at 21:33
  • 1
    Also keep in mind that the windows console window doesn't handle unicode. The _program_ might handle unicode fine, but the output window doesn't. – Mooing Duck Sep 13 '12 at 21:34

2 Answers2

1

As a general rule wchar_t is pointless on non-Windows platforms. On Linux this works fine:

#include <iostream>
#include <string>

int main(void)
{
  std::string d = "Mädchenß_этотязык_ブリザンメント";
  std::cout << d << "\n";  
}

so long as you're not using an ancient configuration that uses something other than the UTF-8 encoding somewhere.

Things are harder on Windows for a number of reasons, but here's one way:

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

int main(void)
{
  _setmode(_fileno(stdout), _O_U16TEXT);
  std::wstring d = L"Mädchenß_этотязык_ブリザンメント";
  std::wcout << d << "\n";  
}
bames53
  • 86,085
  • 15
  • 179
  • 244
  • interesting, thanks with Linux. But I get this in Windows: http://imm.io/EiIX - i.e. only German language is shown correctly – Tebe Sep 14 '12 at 11:38
  • I'd forgotten that the console still had raster fonts. You need to switch to one of the TTF fonts. Lucida Console or Consolas should be available. The Japanese will show up as boxes, but the correct characters will still be there (you can show it by copying and pasting from the console to notepad). – bames53 Sep 14 '12 at 13:25
  • do you mean windows console? it's some problematic to distribute +1 application with my programme – Tebe Sep 16 '12 at 11:58
  • Yes, I'm talking about the Windows console, not some third party program. You just have to change the font settings. – bames53 Sep 16 '12 at 12:13
0

On Windows, you should be able to write this as a literal, save file in UTF-8 (as most source code files are already saved) and then execute nowide's widen(string) just before the output. It will work.

You do not want to save your source code with the literal inside in UTF-16. Another approach would be not to store strings in literals, but in a separate place with a well-defined encoding which will not change with a click of a button in the IDE.

Pavel Radzivilovsky
  • 18,794
  • 5
  • 57
  • 67