views:

272

answers:

2

In linux terminal one would type

locale charmap

in order to see what kind of character-encoding your system uses, eg UTF-8. My question is how would you do this using c/c++. (I'm using linux)

edit: I tried using

nl_langinfo(CODESET)

but I got ANSI_X3.4-1968 instead of UTF-8 (which is what I get when typing: locale charmap). Am I using nl_langinfo() wrong?

+4  A: 
SETLOCALE(3)               Linux Programmer’s Manual              SETLOCALE(3)

NAME
       setlocale - set the current locale

SYNOPSIS
       #include <locale.h>

       char *setlocale(int category, const char *locale);

DESCRIPTION
       The  setlocale() function is used to set or query the program’s current
       locale.


NL_LANGINFO(3)             Linux Programmer’s Manual            NL_LANGINFO(3)

NAME
       nl_langinfo - query language and locale information

SYNOPSIS
       #include <langinfo.h>

       char *nl_langinfo(nl_item item);

DESCRIPTION
       The  nl_langinfo()  function provides access to locale information in a
       more flexible way than localeconv(3) does.  Individual  and  additional
       elements  of  the locale categories can be queried.  setlocale(3) needs
       to be executed with proper arguments before.
       Examples for the locale elements that can be specified  in  item  using
       the constants defined in <langinfo.h> are:

       CODESET (LC_CTYPE)
          Return  a string with the name of the character encoding used in
          the  selected  locale,  such  as   "UTF-8",   "ISO-8859-1",   or
          "ANSI_X3.4-1968"  (better  known as US-ASCII).  This is the same
          string that you get with "locale charmap".  For a list of  char‐
          acter encoding names, try "locale -m", cf. locale(1).
DigitalRoss
nl lang info appears to require you to specify the locale and returns detailed information, it won't tell you what the locale is. Hence why I deleted my answer and upvoted yours :)
Doug T.
Would answer the question better if you included the bit in the manpage where it says `CODESET (LC_CTYPE) ... same string that you get with "locale charmap"`
ephemient
Urk! Good point.
DigitalRoss
This is great! I think it's just what i was looking for! I'll give it a try, thx~~
A: 

Usually, you don't do so explicitly. Instead, you set your program to use a locale named "". That will give you whatever locale the system is set to use.

Jerry Coffin