Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old November 10th, 2006, 08:45 AM
Ralf Goertz
Guest
 
Posts: n/a
Default locale and wcin, wcout, wfstream

Hi,

consider the following program loc.cc

#include <iostream>
#include <fstream>
#include <string>
#include <locale>

using namespace std;

int main(){
char *l=setlocale(LC_ALL,"de_DE.UTF-8");
if (!l) {cerr<<"locale problem"<<endl; return 1;}
wstring w;
wcin>>w;
wostream &outf=wcout; // (*) works
// wofstream outf("xx"); // (**) doesn't work
outf<<w<<L" "<<w.size()<<endl;
return 0;
}


If the program is called with
loc <infile
where infile contains the the UTF-8 encoded line

"The_german_word_for_apples_is_Äpfel"

I get the output

"The_german_word_for_apples_is_Äpfel 35"

However, if the line (*) is commented out and instead the line (**) is
used the file xx is empty. This can be fixed by

outf.imbue(locale("de_DE.UTF-8"));

after the the declaration of outf and before outputting the string. On
the other hand omitting the "setlocale" call and instead imbuing wcin
with the locale directly yields

"The_german_word_for_apples_is_ 30"


This seems a little odd. Does that mean wcin and wcout are locale-aware
after a setlocale call whereas "ordinary" files need an imbue call? If
so why?
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles