0

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?

Edited by triumphost

2
Contributors
3
Replies
23
Views
3 Years
Discussion Span
Last Post by Ancient Dragon
0

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;
}

Edited by Ancient Dragon

0

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

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

Edited by triumphost

0

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;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.