代码:
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
void GetLocalTime(void)
{
time_t rawtime;
struct tm *timeInfo;
char newFileName[30] = {0};
memset(newFileName, 0, sizeof(newFileName));
time(&rawtime);
timeInfo = localtime(&rawtime);
sprintf(newFileName, "./videos");
sprintf(newFileName + strlen(newFileName), "/");
sprintf(newFileName + strlen(newFileName), "fire_%d_%d", 12, 16);
sprintf(newFileName + strlen(newFileName), "_%04d_%02d_%02d_%02d_%02d_%02d", timeInfo -> tm_year + 1900, timeInfo -> tm_mon + 1, \
timeInfo -> tm_mday, timeInfo -> tm_hour, timeInfo -> tm_min, timeInfo -> tm_sec);
printf("newFileName : %s\n", newFileName);
}
int main(void)
{
GetLocalTime();
}
执行结果:
guoyanzhang@debian:~/from_share$ gcc test_dir.c -lm
guoyanzhang@debian:~/from_share$ ./a.out
newFileName : ./videos/fire_12_16_2021_01_13_15_26_52