在网上找到这个答案
#include <stdio.h>
#include <dos.h>
int main(void)
{
struct time t;
gettime(&t);
printf("The current time is: %2d:%02d:%02d.%02d\n",
t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund);
return 0;
}
可我复制进去怎么出现这种错误 error: aggregate `time t' has incomplete type and cannot be defined还有 error: `gettime' was not declared in this scope
第1个回答 推荐于2016-11-15
你的代码没有错,在DOS的C下没有问题。但DOS的有些时间函数在C++编译器下已经无效了。可尝试用下面的——
#include "stdio.h"
#include "time.h"
#include <DOS.H>
void main(void){
struct tm *pt;
time_t t;
t=time(NULL);
pt=localtime(&t);
printf("The current time is: %2d:%02d:%02d\n",
pt->tm_hour, pt->tm_min, pt->tm_sec);
}
但tm结构没有ms级变量。追问
#include "stdio.h"
#include "time.h"
#include <DOS.H>
void main(void){
struct tm *pt;
time_t t;
t=time(NULL);
pt=localtime(&t);
printf("The current time is: %2d:%02d:%02d\n",
pt->tm_hour, pt->tm_min, pt->tm_sec);
}
但tm结构没有ms级变量。追问
怎么不断刷新时间
追答把这段代码写在一个程序的大循环里才行。
本回答被提问者和网友采纳第2个回答 2014-11-03
#include<stdio.h>
#include<time.h>
int main(void)
{
tm t;
_getsystime(&t);
printf("The current time is: %2d:%02d:%02d\n", t.tm_hour, t.tm_min, t.tm_sec);
return 0;
}追问
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
#include<time.h>
int main(void)
{
tm t;
_getsystime(&t);
printf("The current time is: %2d:%02d:%02d\n", t.tm_hour, t.tm_min, t.tm_sec);
return 0;
}追问
tm是时间类型么
追答这是一个系统定义好的结构体,专门用来保存时间的,在time.h中声明,直接使用即可。
struct tm {int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
第3个回答 2014-11-03
gettime C语言里没有这函数!追问
好的我试试
追答别试了 C语言里没这函数 要用就用gettimeofday或time函数
追问,,,,我说怎么不行