关于使用 strcat 格式化输出
我需要一些帮助来使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要编写一个辅助函数,给定一个整数
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 digitsd
, and a buffer which is 'big enough' (at least n+1 bytes long), does the work of formatting '%04d', etc. This function might usestrcat()
, 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 usestrcat()
.)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()
(orstrncat()
) 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 tostrncat()
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.