关于使用 strcat 格式化输出

发布于 2024-10-01 13:58:44 字数 693 浏览 0 评论 0原文

我需要一些帮助来使用 strcat 函数而不是 s 转换以下格式 打印。

const char* const MSG_STAMP_PRINTF_FORMAT = "%c %04d-%02d-%02d %02d:%02d:%02d.%03d";

char cMsgStamp[500];
char cSevChr = 'I'; 

struct tm gmt;

// Calculate Day/Hour/Min/Sec
gmtime_r((time_t *)&pMsg->iSysTimeSec, &gmt);

int iSysTimeMs = 100;

// Format the begining of the message, the message stamp

sprintf(&cMsgStamp[0],
         MSG_STAMP_PRINTF_FORMAT,
         cSevChr, gmt.tm_year+1900, gmt.tm_mon + 1, gmt.tm_mday, gmt.tm_hour, gmt.tm_min, gmt.tm_sec,iSysTimeMs
             ); is 0x%s\n", n3);

我必须使用 strcat 函数的数量来获取上面 cMsgStamp 中存在的相同信息,而不是使用 sprintf 。

任何人都可以帮我解决这个问题吗? 谢谢! 文卡塔·RKA

I need some help in converting following format using strcat function instead of s
printf.

const char* const MSG_STAMP_PRINTF_FORMAT = "%c %04d-%02d-%02d %02d:%02d:%02d.%03d";

char cMsgStamp[500];
char cSevChr = 'I'; 

struct tm gmt;

// Calculate Day/Hour/Min/Sec
gmtime_r((time_t *)&pMsg->iSysTimeSec, &gmt);

int iSysTimeMs = 100;

// Format the begining of the message, the message stamp

sprintf(&cMsgStamp[0],
         MSG_STAMP_PRINTF_FORMAT,
         cSevChr, gmt.tm_year+1900, gmt.tm_mon + 1, gmt.tm_mday, gmt.tm_hour, gmt.tm_min, gmt.tm_sec,iSysTimeMs
             ); is 0x%s\n", n3);

instead of using sprintf, i have to get same info which is present in cMsgStamp above using number of strcat functions.

can any one help me on this.
Thanks!
Venkata RKA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

汐鸠 2024-10-08 13:58:44

您需要编写一个辅助函数,给定一个整数 n、一些数字 d 以及一个“足够大”的缓冲区(至少 n+1字节长),执行格式化“%04d”等工作。此函数可能会使用 strcat(),但也可能不会。

您需要一个可以单步执行格式字符串、隔离不同位(基本上有%序列和其他字符)并适当处理它们的主函数。这可能会使用 strcat(),但除非您使用两个缓冲区,否则可能不会使用 - 一个用于保存第一个(辅助)函数的临时结果,另一个用于保存最终结果。 (我认为没有必要使用两个缓冲区,但它会提供使用 strcat() 的借口。)

确保 main 函数被告知缓冲区有多大并且它不会溢出其缓冲区的边界。仅当您准确地知道要复制到的缓冲区中有多少数据、有多少数据时,才可以安全地使用 strcat() (或 strncat())在您要添加的字符串中,以及您正在使用的缓冲区总共有多长。特别是,strncat() 的接口会带来灾难——最后一个(大小)参数不是目标缓冲区的大小(就像在大多数类似的调用中一样);它是缓冲区中剩余的空间量。

You need to write one auxilliary function that, given an integer number n, a number of digits d, and a buffer which is 'big enough' (at least n+1 bytes long), does the work of formatting '%04d', etc. This function might use strcat(), but probably wouldn't.

You need a main function that can step through the format string, isolating different bits (basically, there are %-sequences and other chars) and processing them appropriately. This might use strcat(), but probably wouldn't unless you have two buffers in use - one to hold temporary results from the first (auxilliary) function and one for the final result. (I don't see a need to use the two buffers, but it would provide an excuse to use strcat().)

Make sure that the main function is told how big the buffer is and that it does not overflow the bounds of its buffer. It is only ever safe to use strcat() (or strncat()) if you know precisely how much data is in the buffer you are copying to, how much data is in the string you are adding, and precisely how long the buffer you are using is altogether. And, in particular, the interface to strncat() invites disaster - the last (size) argument isn't the size of the target buffer (like it is in most similar calls); it is the amount of space left in the buffer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文