简体   繁体   中英

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.

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 newfilename[PATH_MAX];

I would like to finally have a char array that will be :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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