【发布时间】:2016-09-01 13:47:52
【问题描述】:
我有一个简单函数的问题(我猜是因为一些错误的指针分配)。由于strptime 函数(一个接受字符串并返回带有所有数据集的struct tm 的函数)在 Windows 中不存在,我通过调用其他基本工作函数创建了一种 strptime 函数。
这是测试代码。在 STRPTIME 函数中,时间设置得很好,而在 main 中我丢失了信息。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void STRPTIME(char *strtm, struct tm *tminfo)
{
time_t rawtime;
int day, month, year;
sscanf(strtm, "%d-%d-%d\n", &year, &month, &day);
time( &rawtime );
tminfo = localtime( &rawtime );
tminfo->tm_year = year - 1900;
tminfo->tm_mon = month - 1;
tminfo->tm_mday = day;
mktime(tminfo);
printf("date %d-%d-%d\n", tminfo->tm_year, tminfo->tm_mon, tminfo->tm_mday);
}
int main()
{
char stm[11];
struct tm tminfo;
strcpy(stm, "2015-12-31");
STRPTIME(stm, &tminfo);
printf("tminfo %d-%d-%d\n", tminfo.tm_year, tminfo.tm_mon, tminfo.tm_mday);
return(0);
}
【问题讨论】:
标签: c pointers time struct strptime