How to write a UTF-8 encoded string to a file in windows, in C++

Recently I solved a problem of reading and writing UTF8 text for multiple cultures (like Japan, China, France, etc.) in C++. And I've posted this example on stackoverflow (original link). Here is just a copy of my elder link.

It is easy if you use C++11 standard. But if you want to create multiplatform code with elder standards, you can use this method (like I used):

- Read the following article: Code Project - Reading UTF-8 with C++ streams

- Add `stxutif.h` to your project from sources above

- Open file in ANSI mode and add BOM to the start of a file first of all, like this:

   std::ofstream fs;
   fs.open(filepath, std::ios::out|std::ios::binary);
   unsigned char smarker[3];
   smarker[0] = 0xEF;
   smarker[1] = 0xBB;
   smarker[2] = 0xBF;
   fs<   fs.close();

- Then open file as UTF and write your content there:

   std::wofstream fs;
   fs.open(filepath, std::ios::out|std::ios::app);
   std::locale utf8_locale(std::locale(), new utf8cvt);
   fs.imbue(utf8_locale);
   fs << .. //write anything you want into this stream

So, I think this will be helpful for those who met problems like this.