By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,513 Members | 1,629 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,513 IT Pros & Developers. It's quick & easy.

help: %*s in sprintf

P: 1
hi, all:

I had a question about the formatted string while using sprintf(), can %*s truncate a string if the size of it exceeds? for example:
sprintf(zcMessageText, "%ld : %*s", nMyErrMsgKey,SYSMSG_SIZE-25, zcMessage);
if the size of zcMessage exceeds SYSMSG_SIZE, can we use sprintf with %*s to make buffer not overflow?
In my understanding, it only formats string if its size is less than or equal to the width specified, otherwise, just try to put everything in.
Please give me some advice on that, thanks.
Jun 1 '07 #1
Share this Question
Share on Google+
2 Replies


gpraghuram
Expert 100+
P: 1,275
hi,
If you want to limit the size of string being printed to the buffer then you should use snprintf

Thanks
Raghuram
Jun 1 '07 #2

AdrianH
Expert 100+
P: 1,251
You method should work, so would snprintf().

Hmmm, I just tested %.*s on g++ 3.4.4 and it didn't work. As far as I know it should work. :(

The only problem with snprintf() is that I've found not all compilers have it (or have it hidden). I'm not sure how previlant this problem is though.


Adrian
Jun 1 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.