
C语言关于localtime_s()和asctime_s()两个函数的用法。 30
我是个C语言初学者,请教各位C高手,关于localtime_s()和asctime_s()的问题。在visualstudio6.0里面,可以直接用localtime()把...
我是个C语言初学者,请教各位C高手,关于localtime_s()和asctime_s()的问题。
在visual studio 6.0里面,可以直接用localtime()把时间变量转换成结构体,然后再用 asctime()把这个结构体转换成ASCII码,然后可以用于显示。
比如:
#include <stdio.h>
#include <time.h>
void get_time(void);
int main()
{
get_time();
return 0;
}
void get_time(void)
{
time_t now;
time(&now);
printf("Current time is: %s\n",
asctime(localtime(&now)));
}
这个程序可以显示当前时间。
但是在visual studio .net 2005里面,有很多涉及到安全性的问题,很多函数都有变化,比如strcpy()就变成strcpy_s(),而且参数也多了一个。现在的问题是,localtime()变成了localtime_s(),asctime()变成了asctime_s(),而且函数的参数也变了。我看了help的讲解,但是还是用不对这两个函数。请各位C高手给予指点,给我讲讲新老格式的区别,并且将我上面的程序用localtime_s()和asctime_s()改写一下。万分感谢。恳请各位C高手指导我这个菜鸟一下。
嗯,是的,就是类型被强化以后,我总是改不对了。还请03013110这位高手能帮我把上面那段程序用localtime_s()和asctime_s()改写一下。要不然我还是不能明白。多谢了。 展开
在visual studio 6.0里面,可以直接用localtime()把时间变量转换成结构体,然后再用 asctime()把这个结构体转换成ASCII码,然后可以用于显示。
比如:
#include <stdio.h>
#include <time.h>
void get_time(void);
int main()
{
get_time();
return 0;
}
void get_time(void)
{
time_t now;
time(&now);
printf("Current time is: %s\n",
asctime(localtime(&now)));
}
这个程序可以显示当前时间。
但是在visual studio .net 2005里面,有很多涉及到安全性的问题,很多函数都有变化,比如strcpy()就变成strcpy_s(),而且参数也多了一个。现在的问题是,localtime()变成了localtime_s(),asctime()变成了asctime_s(),而且函数的参数也变了。我看了help的讲解,但是还是用不对这两个函数。请各位C高手给予指点,给我讲讲新老格式的区别,并且将我上面的程序用localtime_s()和asctime_s()改写一下。万分感谢。恳请各位C高手指导我这个菜鸟一下。
嗯,是的,就是类型被强化以后,我总是改不对了。还请03013110这位高手能帮我把上面那段程序用localtime_s()和asctime_s()改写一下。要不然我还是不能明白。多谢了。 展开
3个回答
展开全部
1、localtime函数:
原型:struct tm * localtime(const time_t * clock);
功能:把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间, 其中clock为秒数时间;
返回值:返回一个tm结构体的指针。tm结构体是time.h中定义的用于分别存储时间的各个量(年月日等)的结构体。
2、asctime函数:
原型:char* asctime (const struct tm * timeptr);
功能:把timeptr指向的tm结构体中储存的时间转换为字符串;
返回值:一个固定格式的字符串。字符串格式为:Www Mmm dd hh:mm:ss yyyy。其中Www为星期,Mmm为月份,dd为日,hh为时,mm为分,ss为秒,yyyy为年份;
3、例程:
#include<time.h>
#include<stdio.h>
int main(){
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);//使用localtime函数把秒数时间rawtime转换为本地时间以tm结构体保存,并把tm结构体地址储存到timeinfo当中
printf("当前日期为: %s",asctime(timeinfo));//使用asctime函数把tm结构体中储存的时间转换为字符串,并输出
return 0;
}
展开全部
#include <stdio.h>
#include <time.h>
#define TIME_MAX 32
void get_time(void);
int main()
{
get_time();
getchar();
return 0;
}
void get_time(void)
{
time_t now;
time(&now);
// 定义两个变量,存储转换结果
struct tm tmTmp;
char stTmp[TIME_MAX];
// 转换为tm结构
localtime_s(&tmTmp,&now);
// 转换为字符串并输出
asctime_s(stTmp,&tmTmp);
printf("Current time is: %s\n",stTmp);
}
#include <time.h>
#define TIME_MAX 32
void get_time(void);
int main()
{
get_time();
getchar();
return 0;
}
void get_time(void)
{
time_t now;
time(&now);
// 定义两个变量,存储转换结果
struct tm tmTmp;
char stTmp[TIME_MAX];
// 转换为tm结构
localtime_s(&tmTmp,&now);
// 转换为字符串并输出
asctime_s(stTmp,&tmTmp);
printf("Current time is: %s\n",stTmp);
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
看帮助还不明白?不至于吧……
所谓的_s不过是把类型稍微的强化了一下,还有就是长度的控制,以防溢出而已……
所谓的_s不过是把类型稍微的强化了一下,还有就是长度的控制,以防溢出而已……
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询