Using wstring and wchar_t in c ++, there was a problem outputting intermediate information to the console. Project coding costs UTF-8. If you change to UTF-16, then the problem will remain, but hieroglyphs will be added.

 wcout << L"Text"; // Выводится нормально. wcout << L"Текст на русском"; // Не выводится. wcout << L"Text"; // Не выводится, хотя уже английские буквы. cout << "Выводится"; // cout работает нормально. 

There is a use of cout , then everything is fine, but my data is stored in wchar_t and wstring so cout does not fit.

The windows.h library on mac os is not available, so SetConsoleSp and so on are not suitable. setlocale didn't help either.

I tried to change the font and encoding, to combine them, but nothing helps.

PS I understand that there were already similar questions on the output to the console, but following those instructions I could not make a readable result for processing and output to the console.

Test code:

 #include <iostream> #include <string> #include <clocale> using namespace std; int main() { wcout << L"Text" << endl; wcout << L"Русский текст" << endl; wcout << L"Text" << endl; return 0; } 

At the output (terminal mac os) only the word Text.

  • What platform and compiler? - mymedia
  • IDE - CLion from Jetbrains . The compiler - CMake (there from xcode 'a) - kot_mapku3
  • And what is your OS? Poppy? The idea is to go through the general Linux decision. - VladD
  • @VladD Mac Os Sierra - kot_mapku3 February
  • one
    Windows has no locale support. Therefore it is necessary to separate the implementation for Windows separately, for * nix separately. - Majestio

1 answer 1

Try this:

 // в вашем случае, аналогично вашему последнему варианту // за исключением того, что кодировка строкового литерала // указывается явно cout << u"Текст на русском"; 

Only the question does not specify the encoding (or rather the locale) in the terminal, and this is significant.

To convert std::wstring to std::string (for UTF8 ), advise:

 std::string wstring_to_utf8 (const std::wstring& str) { std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv; return myconv.to_bytes(str); } 

Addition

An example of working modified code (tested under macOS Sierra 10.12.3 ):

 #include <iostream> #include <codecvt> std::string wtos(const std::wstring& str) { std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv; return myconv.to_bytes(str); } std::ostream& operator<<(std::ostream& stream, const std::wstring& str) { std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv; stream << myconv.to_bytes(str); return stream; } int main() { std::wstring S1 = L"Text"; std::wstring S2 = L"Русский текст"; std::wstring S3 = L"Text"; // Вариант вывода через функцию std::cout << wtos(S1) << std::endl; std::cout << wtos(S2) << std::endl; std::cout << wtos(S3) << std::endl; // Вариант вывода через перегрузку оператора << std::cout << S1 << std::endl; std::cout << S2 << std::endl; std::cout << S3 << std::endl; return 0; } 

Displays:

 Text Русский текст Text Text Русский текст Text 
  • after the Russian text, such a cout 's 0x10c5c1e8c is this: 0x10c5c1e8c (must be "text" , without quotes); - kot_mapku3
  • What is the locale in your terminal? - Majestio
  • Did not understand a little and I do not have codevt_utf8 in std. - kot_mapku3
  • In the terminal type locale and show the output of this command. - Majestio
  • LANG="ru_RU.UTF-8" LC_COLLATE="ru_RU.UTF-8" LC_CTYPE="ru_RU.UTF-8" LC_MESSAGES="ru_RU.UTF-8" LC_MONETARY="ru_RU.UTF-8" LC_NUMERIC="ru_RU.UTF-8" LC_TIME="ru_RU.UTF-8" LC_ALL= - terminal output. But maybe the built-in terminal in the IDE works differently somehow. - kot_mapku3