无法将类型“ locatime_s”的值分配给类型“ tm *”的实体

时间:2019-10-26 12:23:22

标签: visual-c++

我想知道如何解决此代码,似乎之间存在冲突 “ locatime_s或具有time_t和tm *的localtime_r。我尝试使用localtime, 但它会产生错误“ localtime是不安全的函数,请尝试使用 localtime_r或localtime_s。

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

此代码应显示位置,日期,Currenty符号, 并更改语言环境和Currenty符号。

1 个答案:

答案 0 :(得分:0)

这是固定代码,该代码可在Visual Studio 2019中使用。感谢您的注释帮助,我希望这篇文章可以帮助其他人。

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

int main()
{
    time_t rawtime;
    struct tm timeinfo;
    char buffer[40];
    errno_t err;
    struct lconv* lc;

    time(&rawtime);  //Gets the current time.
    err = localtime_s(&timeinfo,&rawtime);

    int twice = 0;

    do {
        printf("Locale is: %s\n", setlocale(LC_ALL, NULL));

        strftime(buffer, 40, "%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++);
}