gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:54:04 27 4
gpt4 key购买 nike

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

我希望最终有一个字符数组:

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

经过多次寻找,我已经精疲力尽了。您能提供任何帮助吗?

那就太棒了!

最佳答案

尝试

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

关于C:将年/月...转换为字符串(字符数组)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15190029/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com