以下C语言程序是linux服务器中记录日志的一种方法,系统每一分钟会向test.txt写入一条记录,代码中使用了tm结构体。试通过网上查找资料,给出tm结构体的原型,并对其中的成员进行标注,不需要对程序进行解释或说明。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <windows.h>
int main(int argc,const char *argv[])
{
FILE *file;
struct tm *t1;
time_t t;
char buf[100];
int line=1;
int c;
memset(buf,0,sizeof(buf));
if ((file=fopen("test.txt","a+"))<0)
{
perror("failed to open test.txt");
return(-1);
}
while ((c=getc(file))!=EOF)
if (c=='\n')
line++;
while(1)
{
time(&t);
t1=localtime(&t);
sprintf(buf,"%d,%d-%d-%d %d:%d:%d\n",line++,t1->tm_year + 1900,t1->tm_mon + 1,t1->tm_mday,t1->tm_hour,t1->tm_min,t1->tm_sec);
fwrite(buf,sizeof(char),strlen(buf),file);
fflush(file);
Sleep(6000);
}
return 0;
}
就是让你把struct tm的结构列出来而已。如下:
struct tm {
int tm_sec; /* 秒 – 取值区间为[0,59] */
int tm_min; /* 分 - 取值区间为[0,59] */
int tm_hour; /* 时 - 取值区间为[0,23] */
int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */
int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
int tm_year; /* 年份,其值等于实际年份减去1900 */
int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的时候,tm_isdst为0;不了解情况时,tm_isdst()为负。
long int tm_gmtoff; /*指定了日期变更线东面时区中UTC东部时区正秒数或UTC西部时区的负秒数*/
const char *tm_zone; /*当前时区的名字(与环境变量TZ有关)*/
};
你需要知道什么啊?