gpt4 book ai didi

c - nl_langinfo() - 信息太多

转载 作者:太空宇宙 更新时间:2023-11-04 03:53:09 24 4
gpt4 key购买 nike

函数 nl_langinfo(INT_CURR_SYMBOL) 返回一个指向格式为字符串常量的指针:

$(THREE-LETTER-PSEUDOACRONYM) $(SIGN)$(SYMBOL)

所以在我的语言环境中 (en_GB.UTF-8),这将是 "GBP -£"。我只想要前三个字母,那么除了将 nul 字符分配给第三个元素或使用 strncpy() 之外,还有其他方法可以做到这一点吗?

strcpy(int_curr_symbol, nl_langinfo(INT_CURR_SYMBOL));  // "GBP -£"
int_curr_symbol[3] = '\0';

// or

strncpy(int_curr_symbol, nl_langinfo(INT_CURR_SYMBOL), 3)); // "GBP"

此外,nl_langinfo(CRNCYSTR),也返回符号和符号,而只需要符号。 “-£” -> “£”

最佳答案

INT_CURR_SYMBOL 似乎是 GNU 扩展。相反,我会建议 localeconv()struct lconvint_curr_symbol 字段。这是来自该字段的 POSIX 规范:

The international currency symbol applicable to the current locale. The first three characters contain the alphabetic international currency symbol in accordance with those specified in the ISO 4217:1995 standard. The fourth character (immediately preceding the null byte) is the character used to separate the international currency symbol from the monetary quantity.

如您所见,它应该包含您得到的内容。

关于您的第二种方法:

Additionally, nl_langinfo(CRNCYSTR), also returns the sign and the symbol, whereas only the symbol in needed. "-£" -> "£"

这是因为符号定义了符号是放在值 (+) 之前、值 (-) 之后,还是应该替换基数字符 (.)。这来自 langinfo.h 的 POSIX 规范:

Local currency symbol, preceded by '-' if the symbol should appear before the value, '+' if the symbol should appear after the value, or '.' if the symbol should replace the radix character. If the local currency symbol is the empty string, implementations may return the empty string ( "" ).

关于获取字符串部分的方法,我建议在调用后立即将返回值中的相关部分复制到您自己的缓冲区中。您必须这样做,因为由任一函数返回的结构和各种指针是由 C 库分配和维护的。

POSIX 为 nl_langinfo() 给出的原因返回值:

The application shall not modify the string returned. The pointer returned by nl_langinfo() might be invalidated or the string content might be overwritten by a subsequent call to nl_langinfo() in any thread or to nl_langinfo_l() in the same thread or the initial thread, by subsequent calls to setlocale() with a category corresponding to the category of item (see ) or the category LC_ALL, or by subsequent calls to uselocale() which change the category corresponding to the category of item. The pointer returned by nl_langinfo_l() might be invalidated or the string content might be overwritten by a subsequent call to nl_langinfo_l() in the same thread or to nl_langinfo() in any thread, or by subsequent calls to freelocale() or newlocale() which free or modify the locale object that was passed to nl_langinfo_l().

这里是 localeconv() 的类似原因:

The localeconv() function shall return a pointer to the filled-in object. The application shall not modify the structure to which the return value points, nor any storage areas pointed to by pointers within the structure. The returned pointer, and pointers within the structure, might be invalidated or the structure or the storage areas might be overwritten by a subsequent call to localeconv(). In addition, the returned pointer, and pointers within the structure, might be invalidated or the structure or the storage areas might be overwritten by subsequent calls to setlocale() with the categories LC_ALL, LC_MONETARY, or LC_NUMERIC, or by calls to uselocale() which change the categories LC_MONETARY or LC_NUMERIC.

关于c - nl_langinfo() - 信息太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19231795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com