locale in c++

K

kmw

Hi,

I am confronted with an unexpected behavior of std::locale. Consider
the following small example:

#include <locale>
#include <sstream>
#include <isotream>
#include <locale.h>

int main ( int argc, const char** argv ) {
std::locale* loc;
try {
loc = new std::locale ( argv[1] );
}
catch ( std::runtime_error e ) {
std::cerr << e.what ( ) << std::endl;
loc = new std::locale ( );
}
//setlocale ( LC_ALL, loc->name ().c_str () );
std::wistringstream wsin ( L"Äsop" );
wsin.imbue ( *loc );
std::wcout.imbue ( *loc );
wint_t act;
while ( wsin.good () ) {
act = wsin.get ();
std::wcout << std::isalpha<wchar_t> ( (wchar_t)act, *loc ) <<
L" " << (wchar_t)act << std::endl;
}
return 0;
}

Running as "./locale_test en_US.UTF-8" gives me
1 ?
1 s
1 o
1 p
0
If I uncomment the 'setlocal' line output is
1 Ä
1 s
1 o
1 p
0

But why? I understood that 'imbueing' std::cout should do the trick of
printing wide characters. If anybody has a clue, please tell me.

Many thanks in advance,
Kay
 
R

Ralf Goertz

kmw said:
Hi,

I am confronted with an unexpected behavior of std::locale. Consider
the following small example:

#include <locale>
#include <sstream>
#include <isotream>
#include <locale.h>

int main ( int argc, const char** argv ) {
std::locale* loc;
try {
loc = new std::locale ( argv[1] );
}
catch ( std::runtime_error e ) {
std::cerr << e.what ( ) << std::endl;
loc = new std::locale ( );
}
//setlocale ( LC_ALL, loc->name ().c_str () );
std::wistringstream wsin ( L"Äsop" );
wsin.imbue ( *loc );
std::wcout.imbue ( *loc );
wint_t act;
while ( wsin.good () ) {
act = wsin.get ();
std::wcout << std::isalpha<wchar_t> ( (wchar_t)act, *loc ) <<
L" " << (wchar_t)act << std::endl;
}
return 0;
}

Running as "./locale_test en_US.UTF-8" gives me
1 ?
1 s
1 o
1 p
0
If I uncomment the 'setlocal' line output is
1 Ä
1 s
1 o
1 p
0

But why? I understood that 'imbueing' std::cout should do the trick of
printing wide characters. If anybody has a clue, please tell me.

I think you need to call

ios_base::sync_with_stdio(false);

at the beginning of your main function. At least that helped me in a
similar situation with a g++ compiled program.
 
K

kmw

Okay, this works. Many thanks. But do you have any ideas why?

Best regards,
Kay

I think you need to call

ios_base::sync_with_stdio(false);

at the beginning of your main function. At least that helped me in a
similar situation with a g++ compiled program.
I am confronted with an unexpected behavior of std::locale. Consider
the following small example:
#include <locale>
#include <sstream>
#include <isotream>
#include <locale.h>
int main ( int argc, const char** argv ) {
    std::locale* loc;
    try {
        loc = new std::locale ( argv[1] );
    }
    catch ( std::runtime_error e ) {
        std::cerr << e.what (  ) << std::endl;
        loc = new std::locale (  );
    }
    //setlocale ( LC_ALL, loc->name ().c_str () );
    std::wistringstream wsin ( L"Äsop" );
    wsin.imbue ( *loc );
    std::wcout.imbue ( *loc );
    wint_t act;
    while ( wsin.good () ) {
        act = wsin.get ();
        std::wcout << std::isalpha<wchar_t> ( (wchar_t)act, *loc ) <<
L" " << (wchar_t)act << std::endl;
    }
    return 0;
}
Running as "./locale_test en_US.UTF-8" gives me
1 ?
1 s
1 o
1 p
0
If I uncomment the 'setlocal' line output is
1 Ä
1 s
1 o
1 p
0
But why? I understood that 'imbueing' std::cout should do the trick of
printing wide characters. If anybody has a clue, please tell me.
 
R

Ralf Goertz

kmw said:
Okay, this works. Many thanks. But do you have any ideas why?

This flag determines whether C and C++ I/O operations use common buffers
or not. g++ turns it on by default. This leads to all kinds of problems
when imbueing wstreams. In C++ you can imbue wcin with one locale and
wcout with another whereas in C you have just one global locale for the
whole program AFAIK. Therefore, whenever I have to deal with wide
streams I turn that flag off.

Ralf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top