简体   繁体   English

C:将年/月…转换为字符串(字符数组)?

[英]C: Convert the year/month… into a string (array of char)?

In C programming language for gcc (under linux and windows xp), I have a function that does output me several information. 在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

I have an array of char that is a: 我有一个char数组,它是:

char newfilename[PATH_MAX];

I would like to finally have a char array that will be : 我想最后有一个char数组将是:

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

I am out of energy after many searching. 经过多次搜寻,我精神不振。 COuld you please give any help? 你能帮忙吗?

It would be so so great ! 太好了!

Try 尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM