M
M
mrZurg2015-04-17 14:34:40
Programming
mrZurg, 2015-04-17 14:34:40

Why doesn't wstring::c_str() display the Russian part of the text?

The problem is that:
there is a string str - contains a part in English, a part in Russian.
I create wstring strW(str.begin(), str.end());
If you try to output .c_str():
wcout << strW.c_str() ;
Then only the English part is displayed (moreover, wstring::length() - says that the string is correct - uncut length like str)
Tried:
wcin.imbue(locale("rus_rus.866"));
wcout.imbue(locale("rus_rus.866"));

But it:
1 - doesn't work (if only looping to display strW character-by-character - it displays the whole line)
2 - I need it for:
hf = FindFirstFile(strW.c_str(), &fd);
not for wcout

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Adamos, 2015-04-17
@Adamos

Isn't 866 encoding just for the console?
Any functions must accept either unicode or, if it's WinAPI legacy, 1251.

M
monah_tuk, 2015-04-17
@monah_tuk

imbue() applies the locale for I/O operations, such as how to display the fraction separator: dot or comma. At the beginning, before I / O operations, you need to put:

std::locale system("");
std::locale::global(system);

Here is a more complete example that shows the differences:
#include <iostream>
#include <locale>

using namespace std;

int main()
{
    std::locale system("");
    std::locale::global(system);

    wstring str = L"hello, world. привет, МКС";
    wcout << str << endl;
    wcout << str.c_str() << endl;

    wcout << 3.14 << endl;
    // А теперь будет десятичный разделитель согласно локали выводится
    wcout.imbue(system);
    wcout << 3.14 << endl;

    return 0;
}

The conclusion is:
hello, world. привет, МКС
hello, world. привет, МКС
3.14
3,14

UPD: It's also nice to use setlocale() to set the C locale that the C library uses. Yep, they are different and are set independently ( https://stdcxx.apache.org/doc/stdlibug/24-3.html ) :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question