sprintf()

B

Brian Gardels

Does sprintf() have the ability to concatenate or append to a buffer?

Also, does it have any format specifiers for inserting the comma for
thousands separator (12,345 rather than 12345)?

Thanks.
 
M

Morris Dovey

Brian said:
Does sprintf() have the ability to concatenate or append to a buffer?

sprintf(buffer+strlen(buffer),...
Also, does it have any format specifiers for inserting the comma for
thousands separator (12,345 rather than 12345)?

No. It would be more useful if it provided a specifier to use the
locale-specific separator; but the standards committee apparently
didn't see it that way.
 
S

Simon Biber

Brian Gardels said:
Does sprintf() have the ability to concatenate or append to a buffer?

It's possible by giving a pointer to the end of the buffer:
char buf[20] = "Hello, ";
sprintf(strchr(buf, 0), "%d %d %s", 12, 42, "World");

Remember it is undefined behaviour to have it read and modify the same
memory area, such as
sprintf(buf, "insert %s", buf); /* WRONG */
Also, does it have any format specifiers for inserting the comma for
thousands separator (12,345 rather than 12345)?

Not according to the standard. Some implementations have extensions that
allow this sort of thing. If you want it portable, implement it yourself.
 
S

Simon Biber

Simon Biber said:
It's possible by giving a pointer to the end of the buffer:
char buf[20] = "Hello, ";
sprintf(strchr(buf, 0), "%d %d %s", 12, 42, "World");

Of course I meant the end of the string, not the end of the buffer
-- in which case it would certainly overflow the buffer.
 
B

Brian Gardels

I'm laughing at my self right now. The solution to concatenate with
sprintf() must have been to simple for me. Thank you.
 
B

Brian Gardels

Two simple solutions so far and I didn't think of either.
Thank you.

------------------------------------------------------
Brian


Simon Biber said:
Brian Gardels said:
Does sprintf() have the ability to concatenate or append to a buffer?

It's possible by giving a pointer to the end of the buffer:
char buf[20] = "Hello, ";
sprintf(strchr(buf, 0), "%d %d %s", 12, 42, "World");

Remember it is undefined behaviour to have it read and modify the same
memory area, such as
sprintf(buf, "insert %s", buf); /* WRONG */
Also, does it have any format specifiers for inserting the comma for
thousands separator (12,345 rather than 12345)?

Not according to the standard. Some implementations have extensions that
allow this sort of thing. If you want it portable, implement it yourself.
 
L

LibraryUser

Brian said:
Does sprintf() have the ability to concatenate or append to a buffer?

If you look up sprintf, you will find that it returns the count
of characters printed. So the following fragment will allow
appending things:

char buffer[WHATEVER];
char *ptr;

ptr = buffer;
while (something) {
ptr += sprintf(ptr, ....)
}

It is up to you to ensure that buffer is large enough. That
might be included in the coding of "something". You may want to
capture the return of sprintf in a separate variable to check for
errors, etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top