Re: locale in c++
kmw wrote:
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.