I seem to have a problem with locales in C++. When I run my programm from within Eclipse, it all works fine. However, when I try to run from the command line, I keep getting this error:
failure: locale::facet::_S_create_c_locale name not valid
This is the code that triggers the error:
// Set up UTF8 file stream
string fileName = "./sz.txt";
wifstream inFileStream;
try {
setlocale(LC_ALL, "");
inFileStream.open(fileName.c_str());
inFileStream.imbue(locale(""));
if(!inFileStream) {
return EXIT_FAILURE;
}
}
catch (const std::exception &exc) {
wcout << "Error while trying to create UTF8 file stream." << endl;
std::cerr << exc.what() << endl;
if( inFileStream.is_open() )
inFileStream.close();
return EXIT_FAILURE;
}
The output of "locale" gives the following:
LANG="de_DE.UTF-8"
LC_COLLATE="de_DE.UTF-8"
LC_CTYPE="de_DE.UTF-8"
LC_MESSAGES="de_DE.UTF-8"
LC_MONETARY="de_DE.UTF-8"
LC_NUMERIC="de_DE.UTF-8"
LC_TIME="de_DE.UTF-8"
LC_ALL="de_DE.UTF-8"
I have also tried to use "de_DE.UTF-8" as the locale string instead of "" (the way it should be actually), but this gives me the same error.
And strangely enough, the program works fine when run from within Eclipse. I am using g++ to compile from the command line with the following version:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn) Target: x86_64-apple-darwin12.4.0 Thread model: posix
Any idea what might be wrong here?
Cheers,
Martin