On 26 June 2011 16:09, eric wrote: > On Sun, 2011-06-26 at 16:52 +0200, Axel Freyn wrote: >> Hi Eric, >> >> On Sun, Jun 26, 2011 at 07:15:54AM -0700, eric wrote: >> > Dear c/g++ advanced programers: >> > I copied and tried to test a piece simple code which is used for >> > (Hardcoding a Unicode String) from the book ( >> > C++ Cookbook) by D. Ryan Stephens, Christopher Diggins, Jonathan >> > Turkanis, and Jeff Cogswell >> > at Chapter 13, Internationalization, section 1: Hardcoding a Unicode >> > String >> > example 13-1, it can compile and run on my g++4.5.2, but I don't quite >> > satisfy its result >> > >> > -------------------- >> > //Example 13-1 Hardcoding a Unicode string >> > #include <iostream> >> > #include <fstream> >> > #include <string> >> > >> > using namespace std; >> > >> > int main() { >> > >> > // Create some strings with Unicode characters >> > wstring ws1 = L"Infinity: \u221E"; >> > wstring ws2 = L"Euro: \u0128"; >> > >> > wchar_t w[] = L"Infinity: \u221E"; >> > >> > wofstream out("tmp\\unicode.txt"); >> > out << ws2 << endl; >> > wcout << ws2 << endl; >> > } >> As far as I know, you should absolutely NOT use non-ascii characters in >> input/output operations without explicitely specifying the >> encoding/localization to be used. >> In your example, I would thus propose to add after the "wofstream >> out..." a line like >> out.imbue(locale("de_DE.UTF-8")); >> which defines the encoding. >> The following works for me: >> >> #include <iostream> >> #include <fstream> >> #include <string> >> using namespace std; >> int main() { >> wstring ws2 = L"Euro:\x20ac"; >> wofstream out("unicode.txt"); >> out.imbue(locale("de_DE.UTF-8")); >> out << ws2<< endl; >> } >> >> (besides, the Euro-symbol in Unicode is \x20ac) >> >> >> In addition, you SHOULD add error-checking to your code. If you add >> if(not out.good()) >> cerr << "Error while writing " << endl; >> AFTER the line which writes into the file, you'll get an error message .... >> >> HTH, >> >> Axel >> > --------------------------- > Dear Axel: > Thanks your reply. > I copied your code and tried on my system. It compile and run, but > reponse by > --------------------------- > root@eric-laptop:/home/eric/cppcookbook# ./a.out > terminate called after throwing an instance of 'std::runtime_error' > what(): locale::facet::_S_create_c_locale name not valid > Aborted > -------- > Do you konw what can be improved more? You probably don't have the de_DE.UTF-8 locale installed. Try running 'locale -a' to see which locales you have installed and pick a utf8 one, e.g. en_US.utf8 > looking to see your(or any advancer's) suggestion again, thanks a lot > in advance, Eric That word still doesn't mean anything.