以c/c ++创建时间戳的可移植方式

veh*_*zzz 4 c c++

我需要以这种格式生成时间戳yyyymmdd.基本上我想创建一个具有当前日期扩展名的文件名.(例如:log.20100817)

Bra*_*ley 18

strftime

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
  char date[9];
  time_t t = time(0);
  struct tm *tm;

  tm = gmtime(&t);
  strftime(date, sizeof(date), "%Y%m%d", tm);
  printf("log.%s\n", date);
  return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)