setlocale()函数

setlocale()函数是用来配置地域信息的,原本以为这个也是windows函数,结果居然是C++的标准函数,其头文件为<clocale>,按照一般的原则,所有原本C的函数被移植到C++标准库中时,是按照去掉后面的.h,前面加上c这样的原则。举例:<stdio.h>变成<cstdio>,所以我猜<clocale>也是这样,但是没有继续去确认。(从示例2上看,猜想是正确的)

测试程序1:

#include <stdio.h>
#include <clocale>
using namespace std;

void
sr_set_locale (void)
{
    setlocale (LC_ALL, "");
    setlocale (LC_CTYPE, "");
    printf ("LOCALE is %s\n",setlocale(LC_ALL,NULL));
}

int main()
{
	sr_set_locale();
	return 0;
}

根据你系统地域设置的不同,你得到的结果也不同,我这里运行得到的结果为:

LOCALE is Chinese_People's Republic of China.936

 

测试程序2:

/* 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) );

    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;
}

输出结果为:

Locale is: C
Date is: 05/06/10 20:51:14
Currency symbol is:
-
Locale is: Chinese_People's Republic of China.936
Date is: 2010-5-6 20:51:14
Currency symbol is: ¥
-

 

 

参考网址:

http://www.cplusplus.com/reference/clibrary/clocale/setlocale/

posted on 2010-05-06 21:26  cnyao  阅读(3628)  评论(0编辑  收藏  举报