void writeLog(char *filename,char *buf) { printf("write log"); FILE *fp; fp = fopen(filename,"at+"); while(NULL != fp) { char now[32]; memset(now,0,sizeof(now)); getTime(now); fwrite(now,strlen(now),1,fp); fwrite(buf,strlen(buf),1,fp); fclose(fp); fp = NULL; } } void getTime(char *buf) { time_t rawtime; struct tm* timeinfo; time(&rawtime); timeinfo = localtime(&rawtime); sprintf(buf,"%04d-%02d-%02d %02d:%02d:%02d :", (timeinfo->tm_year+1900),timeinfo->tm_mon, timeinfo->tm_mday,timeinfo->tm_hour, timeinfo->tm_min,timeinfo->tm_sec); }