I've created:

#include <streambuf>
#include <iostream>
#include <windows.h>


template<typename T>
class BufferedStream : T
{
    private:
        T &stream;
        std::streambuf* buffer; //planning to use this to create other "IO" functions.

    public:
        BufferedStream(T &stream) : stream(stream), buffer(stream.rdbuf()) {}
        ~BufferedStream() {stream.rdbuf(this->buffer);};

        std::ostream& operator << (const char* data);
        std::ostream& operator << (const std::string &data);
        std::wostream& operator << (const wchar_t* data);
        std::wostream& operator << (const std::wstring &data);
};

template<typename T>
std::ostream& BufferedStream<T>::operator << (const char* data)
{
    SetConsoleOutputCP(CP_UTF8);
    DWORD slen = lstrlenA(data);
    WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), data, slen, &slen, nullptr);
    return *this;
}

template<typename T>
std::ostream& BufferedStream<T>::operator << (const std::string &data)
{
    SetConsoleOutputCP(CP_UTF8);
    DWORD slen = data.size();
    WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), data.c_str(), data.size(), &slen, nullptr);
    return *this;
}

template<typename T>
std::wostream& BufferedStream<T>::operator << (const wchar_t* data)
{
    DWORD slen = lstrlenW(data);
    WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), data, slen, &slen, nullptr);
    return *this;
}

template<typename T>
std::wostream& BufferedStream<T>::operator << (const std::wstring &data)
{
    DWORD slen = data.size();
    WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), data.c_str(), slen, &slen, nullptr);
    return *this;
}

BufferedStream<std::ostream> unicout(std::cout);
BufferedStream<std::istream> unicin(std::cin);

int main()
{
    const char s[] = "Русский текст в консоли\n";
    unicout<<s;
}

because of this problem I'm having: http://stackoverflow.com/questions/21370710/unicode-problems-in-c-but-not-c

Anyway, the problem states that I can use unicode in C but not in C++. I have no clue why as I did everything right. I however found that the above class which I wrote to combat this problem works just fine. I want to know if there is a better way to do this at all?

Any ideas?

I usually use the macros in tchar.h to make programs UNICODE friendly. If you search MSDN for the string function it will tell you what macro to use from tchar.h. For example, strcmp() becomes _tcscmp(), and for string literals use the _TEXT macro, such as _TEXT("Hello World");

There are no macros that easily convert character arrays to wide character arrays, use one of the convertion functions for that.

The only suggestion I have for your code is this: (Note: I never heard of unicout but I'm assuming it's part of C++11 standard which I have not studied. But from what I read using google just now c++11 support for UNICODE is pretty poor.)

#include <tchar.h>

....

int main()
{
    const _TCHAR s[] = "Русский текст в консоли\n";
    unicout<<s;
}

lol Ancient, the unicode is actually an instance of the class you see above.

BufferedStream<std::ostream> unicout(std::cout);

If you want it to output unicode strings then std::cout and std::cin won't work because they are not unicode functions. Example:

int main()
{
    wchar_t buf[255] = { 0 };
    wcout << L"Enter something/\n";
    wcin >> buf;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.