c++ setlocale函数的应用

setlocale

配置地域化信息。

语法:   char * setlocale ( int category, const char * locale );

返回值: 字符串

函数种类: 操作系统与环境

内容说明

本函数用来配置地域的信息,设置当前程序使用的本地化信息。参数 category 有下列的选择:

    * LC_ALL 包括下面的全项选项都要。
    * LC_COLLATE 配置字符串比较,PHP 目前尚未实作出来本项。
    * LC_CTYPE 配置字符类别及转换。例如全变大写 strtoupper()。
    * LC_MONETARY 配置金融货币,PHP 目前尚未实作。
    * LC_NUMERIC 配置小数点后的位数。
    * LC_TIME 配置时间日期格式,与 strftime() 合用。

而参数 locale 若是空字符串 "",则会使用系统环境变量的 locale 。若 locale 为零(NULL),则不会改变地域化配置,返回当前的地域值,若系统尚未实作则返回 false。

Locales contain information on how to interpret and perform certain input/output and transformation operations taking into consideration location and language specific settings.

Most running environments have certain locale information set according to the user preferences or localization. But, independently of this system locale, on start, all C programs have the "C" locale set, which is a rather neutral locale with minimal locale information that allows the result of programs to be predictable. In order to use the default locale set in the environment, this function can be called with "" as the locale parameter.

The locale set on start is the same as setlocale(LC_ALL,"C") would set.
The entire default locale can be set by calling setlocale(LC_ALL,"");

C程序开始的时候的设置和 setlocale(LC_ALL,"C")相同

使用系统默认的设置调用setlocale(LC_ALL,"");

The parts of the current locale affected by a call to this function are specified by parameter category.
Return Value
On success, A pointer to a C string identifying the locale currently set for the category. If category is LC_ALL and different parts of the locale are set to different values, the string returned gives this information in a format which may vary between compiler implementations.

Example

/* setlocale example */
#include <stdio.h>
#include <time.h>
#include <locale.h>

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;
  char buffer [80];

  struct lconv * lc;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );

  int twice=0;

  do {
    printf ("Locale is: %s/n", setlocale(LC_ALL,NULL) ); //使用NULL参数,获取当前配置

    strftime (buffer,80,"%c",timeinfo);
    printf ("Date is: %s/n",buffer);

    lc = localeconv ();
    printf ("Currency symbol is: %s/n-/n",lc->currency_symbol);

    setlocale (LC_ALL,"");
  } while (!twice++);
  
  return 0;
}

One of the possible outputs when the previous code is run is:

Locale is: C
Date is: 01/15/07 13:33:47
Currecy symbol is: 
-
Locale is: English_United States.1252
Date is: 1/15/07 1:33:47 PM
Currency symbol is: $

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值