Here I have a small piece of code:

ofstream hello ("hello.txt");
hello << "Γειά σου!"; //hello in greek
hello.close();

And here is the output:

Output on greek windows: "Γειά σου!"
Output on english windows: "ÃåéÜ óïõ!"

Can someone, please, help me find out how to fix this?

Edited 5 Years Ago by SpecialForce: n/a

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!

This question has already been answered. Start a new discussion instead.