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/

原文链接: https://www.cnblogs.com/cnyao/archive/2010/05/06/1729220.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    setlocale()函数

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/10610

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月7日 上午12:01
下一篇 2023年2月7日 上午12:04

相关推荐