In connection with this question I decided to ask another, as it turned out, more important question. In general, I recently started learning C ++ and had problems with encodings, help me overload << from std::cout so that it accepts strings in utf-8 and output them in windows-866.

  • I would recommend the following: (1) if you have Unicode strings (as it should be in the overwhelming majority of cases), use std::wstring and accordingly std::wcout . (2) temporarily switch the console from cp866 mode to utf-8 mode or utf-16 mode : SetConsoleOutputCP . - VladD
  • As far as I remember, a friend uses MinGW, and moreover, there are big problems with locales. So I doubt that wstring will help. - skegg
  • yes I have MinGW - Rules

2 answers 2

Actually, you will be able to overload only if you pass some type other than string, char *, etc., since << cout is already overloaded for them. You can simply create a function with such a signature about

  void printunicode (ostream&, const string&) 

In it, you actually translate the unicode content of the string into the desired encoding, write it into the buffer, and transfer it to the stream.

Or even easier. Create a function that accepts a string with unicode, and returns in the correct encoding. Then you can use it in a series of << operators.

  • 2
    IMHO is more suitable exactly the second (reception in Unicode (or the specified encoding), and output in a given encoding) Naturally, with a chain of calls, there is a problem with the buffers for the recoded string. In principle, it can be solved, but always with certain "pitfalls". - avp pm
  • ..I'm going to try now. I ONLY BEGINED TO LEARN C ++, of course it would be better if you brought a working version, but thanks anyway :) - Rules
  • pancake already tried everything the best way to convert a source file to windows-866 before compiling - Rules
  • @Rules: or maybe you still try wstring and SetConsoleOutputCP? Here's some information for you to think about: * hashcode.ru/questions/179591 * forum.oszone.net/thread-129824-8.html * stackoverflow.com/q/10882277/276994 - VladD
  • 2
    If you have just begun to study C ++, then it is better not to bother with all these encodings. Take care of other, more important and fundamental things. Maybe even better to put in a virtual machine ubuntu and work on it. There's a normal full gcc and all that. At a certain stage, try to implement the function from scratch, as I wrote, which simply translates from one encoding to another: reads the input byte sequence, determines which character is regular and writes it in the corresponding encoding to the output byte buffer. - skegg

I remember zaminusovali when the last time I answered my question but still I will answer :)
I realized that the easiest way to change the encoding of the file to the target (well, I'm not Japanese :)).
In general, for Code :: Blocks did this:
1) I downloaded uniconv.exe (I've been downloading for a long time, I don’t remember where)
1a) Since uniconv does not support utf-8 by default in code :: blocks windows-1251
2) Added the following Tool to Tools:
screen shoot windows Edit tool
3) And if I work with D:\ProjectsC++\C++ Learning\Chapter 2\Homework\Ch2_Ex1.cpp :

 #include <iostream> int main(){ using namespace std; cout << "Привет! " << endl << "Моё имя: Иван." << endl << "Моя фамилия: Черновалов."; cin.get(); return 0; } 

and call tools => reCHARSET WD then I get D:\ProjectsC++\C++ Learning\Chapter 2\Homework\Ch2_Ex1.cpp.cpp in windows 866 then manually :( I open it and see:

 #include <iostream> int main(){ using namespace std; cout << "ЏаЁўҐв! " << endl << "Њ®с Ё¬п: €ў ." << endl << "Њ®п д ¬Ё«Ёп: —Ґа®ў «®ў."; cin.get(); return 0; } 

4) Build & Launch and woo-ala:

screen shoot running program

Can someone help ...

PS: As you can see, I cannot get (without having created the Project) the separate file name separately the directory, so I have to use the double extension to distinguish the files (because I changed the file being edited, this is an application error) and I have to manually open the file.
Also in code :: blocks there are Tools + and Scripts, but I don’t know how to use them if anyone can help improve my "crutch" :) so that a temporary file is created compiled with the current name and deleted ...