Thanks a lot akhena, I found the solution, using what this page uses plus locale! Here is the complete source code:
#include "stdafx.h"
#include <windows.h>
#include <fstream>
#include <string>
#include <locale>
#pragma setlocale("Greek")
using namespace std;
string to_utf8(const wchar_t* buffer, int len)
{
int nChars = ::WideCharToMultiByte(
CP_UTF8,
0,
buffer,
len,
NULL,
0,
NULL,
NULL);
if (nChars == 0) return "";
string newbuffer;
newbuffer.resize(nChars) ;
::WideCharToMultiByte(
CP_UTF8,
0,
buffer,
len,
const_cast< char* >(newbuffer.c_str()),
nChars,
NULL,
NULL);
return newbuffer;
}
string to_utf8(const std::wstring& str)
{
return to_utf8(str.c_str(), (int)str.size());
}
int main()
{
ofstream testFile;
testFile.open("demo.txt", ios::out | ios::binary);
wstring text =
L"Γειά σου!";
string outtext = to_utf8(text);
testFile << outtext;
testFile.close();
return 0;
}
Thanks a lot again!