【发布时间】:2014-02-04 22:43:08
【问题描述】:
我正在为我的 unix 课程用 c 创建一个程序。我正在尝试创建一个函数来查找当前日期,获取个人出生日期(存储在 **individual 字符串中),并使用它们来计算个人年龄(以年和月为单位)。我不确定如何解决这个问题,因为当前日期存储在 struct tm 中,而个人生日存储在字符串中。 (对c很陌生,才学了几天)。下面是我这个函数的代码,如果有人可以请给我一些建议,谢谢。
char* calcage(char **individual, char **age)
{
time_t time_raw_format;
struct tm * time_struct;
char *lastname = (char *)malloc(50*sizeof(char));
char *lnamefile = (char *)malloc(50*sizeof(char));
char buf [100];
time ( &time_raw_format );
time_struct = localtime ( &time_raw_format );
strftime (buf,100,"It is: %m/%d/%Y.",time_struct);
puts (buf);
printf("person: %s\n", *individual);
lastname = strrchr(*individual, ',');
lastname++;
//lnamefile = strtok(lastname, searchseperator);
printf("Individual: %s\n", lastname);
}
【问题讨论】:
-
将字符串中的日期转换为时间,然后做任何你需要做的计算。
-
建议
struct tm time_struct; time_struct = *localtime ( &time_raw_format );