- #include
- #include
- static const intMAX_FILE_NAME_LEN =512;// 文件名长度最大值
- intmain(intargc,char* argv[])
- {
- // 获取当前时间time_t seconds_number = time(NULL);
- tm local_time;
- localtime_s(&local_time, &seconds_number);
- // 当前日期
- chardate[11];
- if(0== strftime(date,sizeof(date),"%F", &local_time))
- {
- printf_s("File:%s Line:%ld 获取日期失败\n", __FILE__, __LINE__);
- return-1;
- }
- // 当前日期时间
- chardate_time[20];
- if(0== strftime(date_time,sizeof(date_time),"%F %T", &local_time))
- {
- printf_s("File:%s Line:%ld 获取日期时间失败\n", __FILE__, __LINE__);
- return-1;
- }
- // 获取日志文件名
- char file_name[MAX_FILE_NAME_LEN];
- // Expression:("Buffer is too small", 0)sprintf_s(file_name,sizeof(file_name),"Common_%s.txt", date);
- // 打开文件FILE * fd;
- if(fopen_s(&fd, file_name,"a") !=0)
- {
- printf_s("File:%s Line:%ld 打开文件%s失败\n", __FILE__, __LINE__, file_name);
- return-1;
- }
- // 打印日志fprintf_s(fd,"%s\n", date_time);
- // 关闭文件
- fclose(fd);
- return 0;
- }
来源: http://www.bubuko.com/infodetail-2055360.html