void writelog(const char* log) { time_t tDate; struct tm* eventTime; time(&tDate);//得到系统当前时间 //将time_数据类型转换为struct tm结构 eventTime = localtime(&tDate); //年,以1900年作为起始值。如果当前年为1991,则year变量=1 int year = eventTime->tm_year + 1900; //月:tm_mon从0开始计算 int month = eventTime->tm_mon + 1; //日: int day = eventTime->tm_mday; //小时 int hour = eventTime->tm_hour; //分钟 int minute = eventTime->tm_min; //秒 int second = eventTime->tm_sec; char sDate[16]; sprintf(sDate, "%04d-%02d-%02d ", year,month,day); char sTime[16]; sprintf(sTime, "%02d:%02d:%02d", hour, minute, second); char s[1024]; sprintf(s, "%s %s %s\n", sDate, sTime, log); FILE* fp = fopen("my.log", "a+"); if (fp==NULL) { printf("log write error :%s", strerror(errno)); } else { fputs(s, fp); fclose(fp); } return; }
原文地址:https://www.cnblogs.com/caoruipeng/p/11830734.html
时间: 2024-10-22 22:02:08