How do you get what encoding your system uses in c / C ++?

In linux terminal type

locale charmap

to find out what character encoding is used by your system, for example UTF-8. My question is: how do you do this using c / C ++. (I am using linux)

to change . I tried to use

nl_langinfo(CODESET)

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

+1
source share
3 answers
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).
+4
source

setlocale(), , locale "C". .

#include <stdio.h>
#include <locale.h>
#include <langinfo.h>

int main()
{
        setlocale(LC_ALL, "");
        char* locstr = setlocale(LC_CTYPE, NULL);
        char* encoding = nl_langinfo(CODESET);
        printf("Locale is %s\n", locstr);
        printf("Encoding is %s\n", encoding);
        return 0;
}
+1

. "". , .

0

Source: https://habr.com/ru/post/1719948/


All Articles