Hi,
how can I add an int from in a loop repetetively , to an string by sprintf().
Hi,
how can I add an int from in a loop repetetively , to an string by sprintf().
Sounds like a homework question...
Hint:
At the end of that, theString = "This is the first part, this is the second part". Now I'm sure you can figure out the restCode:char theString[256] = "This is the first part"; sprintf(theString, "%s, %s" theString, "this is the second part");
you need to use the return value of sprintf http://linux.die.net/man/3/sprintf
I hope you can figure out how to make it in the loopCode:char buf[BUF_SIZ]; int len = 0; int ret = sprintf(buf,"%d ", 1); if(ret < 0) { perror("sprintf"); return ret; } len += ret; ret = sprintf(buf+len, "%d ", 2); etc
Last edited by vart; 08-14-2008 at 10:52 PM.
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
> sprintf(theString, "%s, %s" theString, "this is the second part");
Bad idea.
Like most C functions, this one is NOT safe when you use overlapping source and destination strings (in this case, theString).
Sure, you may get lucky in this case, but try and put anything before the first %s, and you're in for a rude awakening.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Not to mention there's a missing comma.
But you can use something like this . . .
You could even record the length of the string, since strlen() is a bit expensive to compute.Code:sprintf(theString + strlen(theString), ", %s", "this is the second part");
Note that this is ignoring the possibility of buffer overruns, which probably isn't the best idea . . . .Code:int len = 0; /* ... */ while(condition) { len += sprintf(theString + len, ", %s", "this is the second part"); }
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
snprintf() is C99, and the OP said "sprintf". I was just suggesting that you could at least check to make sure len never goes above, say, 10 characters less than the maximum. (Assuming you're not adding more than 10 characters with each sprintf() call.)
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Oops my bad. One of those things I thought was right... thanks for pointing it out