+3  A: 

In Visual studio the output stream is always written in ANSI encoding, and it does not support UTF-8 output.

What is basically need to do is to create a locale class, install into it UTF-8 facet and then imbue it to the fstream.

What happens that code points are not being converted to UTF encoding. So basically this would not work under MSVC as it does not support UTF-8.

This would work under Linux with UTF-8 locale

#include <fstream>
int main()
{
    std::locale::global(std::locale(""));
    std::wofstream fout("myfile");
    fout << L"Հայաստան Россия Österreich Ελλάδα भारत" << std::endl
}

~ And under windows this would work:

#include <fstream>
int main()
{
    std::locale::global(std::locale("Russian_Russia"));
    std::wofstream fout("myfile");
    fout << L"Россия" << std::endl
}

As only ANSI encodings are supported by MSVC.

Codecvt facet can be found in some Boost libraries. For example: http://www.boost.org/doc/libs/1_38_0/libs/serialization/doc/codecvt.html

Artyom
@Artyom: Thanks a lot. But what if I do want it to print the whole string? Any workaround?
Armen Tsirunyan
Yes, install utf-8 codecvt facet. There are such ready facets in Boost. Also there is Boost.Locale library, but the last one would be probably overkill.
Artyom
A: 

MSVC offers the codecvt_utf8 locale facet for this problem.

#include <codecvt>

// ...  
std::wofstream fout(fileName);
std::locale loc(std::locale::classic(), new std::codecvt_utf8<wchar_t>);
fout.imbue(loc);
DerKuchen
@DerKuchen: fatal error C1083: Cannot open include file: 'codecvt': No such file or directory.
Armen Tsirunyan
@Armen Tsirunyan: What version of MSVC do you use? At least for MSVC 2010 it works.
DerKuchen
@DerKuchen: As my post says, I am using MSVC2008. So are you implying I can't do what I want to do with my compiler? :(
Armen Tsirunyan
@Armen Tsirunyan: I'm sorry, I overlooked that. I tried it with MSVC2008 and it seems that this version misses the header.
DerKuchen
I figured out I could download the <codecvt> header, but I can't find it available for download... any ideas if that's possible/legal? OK, whatever I don't care about legal :)
Armen Tsirunyan
I'm not sure if the header alone is enough, it may need some library/runtime support. It should be available in the MSVC 2010 Express Version: http://www.microsoft.com/express/downloads/#2010-Visual-CPP
DerKuchen