How to append strings using sprintf?

user46646 picture user46646 · Apr 20, 2010 · Viewed 158.3k times · Source

I am facing a serious issue with sprintf.

Suppose my code snippet is:

sprintf(Buffer,"Hello World");
sprintf(Buffer,"Good Morning");
sprintf(Buffer,"Good Afternoon");
.
.
.

Some hundred sprints....

If I do like this, its getting overwritten.

How can I avoid overwritting using sprintf. If I give a printf at the end I want to see all the lines.

Answer

anon picture anon · Apr 20, 2010

You need:

sprintf(Buffer,"Hello World");
sprintf(Buffer + strlen(Buffer),"Good Morning");
sprintf(Buffer + strlen(Buffer),"Good Afternoon");

and of course you need your buffer to be big enough.