0

在 gcc 的 C 编程语言中(在 linux 和 windows xp 下),我有一个函数可以输出一些信息。

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 数组,它是:

char newfilename[PATH_MAX];

我想最终有一个 char 数组,它将是:

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

经过多次搜索,我没有精力了。你能帮忙吗?

太好了!

4

1 回答 1

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
于 2013-05-24T05:19:20.767 回答