I have 4 values: A,B,C,D
. After doing a set of computations with those values, I want my code to output the results in a file of the form ABCD_MM.DD.YY.txt
, to keep track of when it was done.
I'm not quite sure on the best way to do this in C. I have a "working" version using itoa()
, which isn't a standard C function and will go (and has gone) unrecognized on machines other than mine when compiling.
This is th开发者_如何学JAVAe code I have for doing this, could someone help with a better (and universally accepted) way? The char array name
was defined with a global scope.
void setFileName(){
time_t now;
struct tm *today;
char date[9];
//get current date
time(&now);
today = localtime(&now);
//print it in DD.MM.YY format.
strftime(date, 15, "%d.%m.%Y", today);
char buff[20];
char vars[20];
//put together a string of the form:
//"ABCD_DD.MM.YY.txt"
strcpy(vars, itoa(A, buff, 20));
strcat(vars, itoa(B, buff, 20));
strcat(vars, itoa(C, buff, 20));
strcat(vars, itoa(D, buff, 20));
strcpy(name, vars);
strcat(name, "_");
strcat(name, date);
strcat(name, ".txt");
}
char filename [ FILENAME_MAX ];
snprintf(filename, FILENAME_MAX, "%d%d%d%d_%s.txt", A, B, C, D, date);
精彩评论