2013-03-03 54 views
0

在用于gcc(linux和windows xp)的C编程语言中,我有一个函数可以输出几个信息。C:将年/月...转换为字符串(char数组)?

pdter(1) gives 2013 (integer) 
pdter(2) gives 3 (for march) (integer) 
pdter(3) gives 3 for day (integer) 
pdter 4 for hours, 5 for min, and 6 for seconds 

我有一个字符数组是一个:

char newfilename[PATH_MAX]; 

我想终于有一个字符数组,这将是:

newfilename of content (array) (date and time) : "20130303204301-image.bmp" 
(format yyyymmddhhmmss-imagename.ext) 

我出的能量后,许多搜索。请给我帮忙吗?

这会太棒了!

+2

读'人strftime' – 2013-03-03 19:48:22

回答

0

尝试

char newfilename[PATH_MAX]; 
char *imagename = TBD; 
#define MAXINTLENGTH (11 /* TBD -2147483648 */) 
// Insure our sprintf does not overrun 
if (6*MAXINTLENGTH + 1 + strlen(imagename) + 1) > PATH_MAX]) { 
    handle potential error; 
} 
int t[6]; 
int seconds; 
// Try once or twice to get all time elements and insure the second has not changed whilst doing so 
for (int attempt = 1; attempt < 2; attempt++) { 
    seconds = pdter(6); 
    for (int i=0; i<6; i++) t[i-1] = pdter(i+1); 
    // If we read all time elements and the second did not change, break; 
    if (seconds == t[5]) break; 
    // Try again 
} 
sprintf(newfilename, "%04d%02d%02d%02d%02d%02d-%s", t[0], t[1], t[2], t[3], t[4], t[5], imagename); 
// We are done