There is a function called strftimethat exists with the explicit purpose of writing the time value to a readable string. Documentation: http://linux.die.net/man/3/strftime
Example:
#include <time.h>
#include <stdio.h>
int main()
{
FILE* file;
char filename[128];
time_t now;
struct tm tm_now;
now = time(NULL);
localtime_r(&now, &tm_now);
strftime(filename, sizeof(filename), "filename_%Y_%m_%d_%H_%M.txt", &tm_now);
file = fopen(filename, "w");
fprintf(file, "Hello, World!\n");
fclose(file);
return 0;
}
source
share