locale and wcin, wcout, wfstream

Discussion in 'C++' started by Ralf Goertz, Nov 10, 2006.

  1. Ralf Goertz

    Ralf Goertz Guest

    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?
     
    Ralf Goertz, Nov 10, 2006
    #1
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maurice Hulsman
    Replies:
    1
    Views:
    2,233
    Guus Bosman
    Jul 25, 2004
  2. Replies:
    2
    Views:
    698
    Mike Wahler
    Mar 24, 2007
  3. Ralf Goertz

    mixing of std::cout and std::wcout

    Ralf Goertz, Jul 4, 2007, in forum: C++
    Replies:
    2
    Views:
    598
    Ralf Goertz
    Jul 6, 2007
  4. Erik Knudsen

    std::wcout and const char *

    Erik Knudsen, Dec 11, 2007, in forum: C++
    Replies:
    10
    Views:
    912
    Erik Knudsen
    Dec 14, 2007
  5. Replies:
    4
    Views:
    1,340
  6. zade
    Replies:
    1
    Views:
    937
    James Kanze
    Mar 5, 2010
  7. Sibylle Koczian
    Replies:
    2
    Views:
    1,382
    Sibylle Koczian
    Nov 20, 2010
  8. Masao Mutoh
    Replies:
    0
    Views:
    218
    Masao Mutoh
    Dec 4, 2008
Loading...