As @AkiRoss said, you need to use locales. In general, in any
program you write, one of the very first actions in main
should be to set the global locale to the users choice, by doing
something like:
std::locale::global( std::locale( "" ) );
This should be systematic, in every program which interacts
with a human user. All files opened after this will
automatically be imbued with the correct locale.
Alternatively, you can explicitly specify the locale you want to
use:
std::locale::global( std::locale( locale_name ) );
The problem with this is that there is no standard for locale
names. Something like "it_IT.UTF-8"
corresponds to the Posix
standard, and is used on the Internet; Windows has traditionally
used a different format (although recent Windows do seem to
accept this format as well).
Which leaves std::cin
, std::cout
and std::cerr
. These are
opened before you enter main
, and so must be imbued with the
new locale. (To obtain a copy of the current global locale, use
the default constructor of std::locale
.)
Finally, if you're opening any binary files, be aware that they
also will be imbued with the global locale. Which may do code
translation. In such cases, you should imbue them explicitly
with std::locale::classic()
, or create a new locale by merging
the codecvt
facet of std::locale::classic()
with the other
facets of the global locale. (std::locale
has special
functions and constructors for this.)