tags:

views:

686

answers:

7

Hey everybody,

Thank you all before I start.

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.

How to xchieve this.

+2  A: 

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.

anon
buffer s large enough...i will try...
+1 although I like Aeth's solution a bit better, it seems a bit more efficient than recalculating the string length every time.
extraneon
A trick I've seen along these lines is to `#define eos(s) ((s)+strlen(s))`, or declare a function if you prefer. Then you can use `sprintf(eos(Buffer), "Stuff")`
spong
+12  A: 
int length = 0;
length += sprintf(Buffer+length, "Hello World");
length += sprintf(Buffer+length, "Good Morning");
length += sprintf(Buffer+length, "Good Afternoon");

Here is a version with some resistance to errors. It is useful if you do not care when errors happen so long as you can continue along your merry way when they do.

int bytes_added( int result_of_sprintf )
{
    return (result_of_sprintf > 0) ? result_of_sprintf : 0;
}

int length = 0;
length += bytes_added(sprintf(Buffer+length, "Hello World"));
length += bytes_added(sprintf(Buffer+length, "Good Morning"));
length += bytes_added(sprintf(Buffer+length, "Good Afternoon"));
Matthew T. Staebler
+1 for using the return value of sprintf.
extraneon
But what happens if sprintf experiences a conversion failure?
anon
Then you have bad things that happen. I omitted the error checking for the sake of brevity.
Matthew T. Staebler
+1 - Additional error checking should be an exercise for the reader anyway. After all, its _their_ code :)
Tim Post
+4  A: 

Why do you want to use sprintf for string concatenation when there are methods intended specifically for what you need such as strcat and strncat?

iPhone beginner
It could be that the example was the trivial case only strings are being appended. The problem can be extended to include cases where you are appending other types of formatted data for which `strcat` would not apply.
Matthew T. Staebler
A: 

What about:

char s[100] = "";

sprintf(s, "%s%s", s, "s1");

sprintf(s, "%s%s", s, "s2");

sprintf(s, "%s%s", s, "s3");

printf("%s", s);

But take into account possible buffer ovewflows!

Jordi
its not appending....
It is probably not a safe move to be using `s` as a source to be read from as well as a destination to be written to. I would liken it to calling `strcpy(s, s)`.
Matthew T. Staebler
+4  A: 

For safety (buffer overflow) I recommend to use snprintf()

const int MAX_BUF = 1000;
char* Buffer = malloc(MAX_BUF);

int length = 0;
length += snprintf(Buffer+length, MAX_BUF-length, "Hello World");
length += snprintf(Buffer+length, MAX_BUF-length, "Good Morning");
length += snprintf(Buffer+length, MAX_BUF-length, "Good Afternoon");
oraz
+1  A: 

Are you simply appending string literals? Or are you going to be appending various data types (ints, floats, etc.)?

It might be easier to abstract this out into its own function (the following assumes C99):

#include <stdio.h>
#include <stdarg.h>
#include <string.h>

int appendToStr(char *target, size_t targetSize, const char * restrict format, ...)
{
  va_list args;
  char temp[targetSize];
  int result;

  va_start(args, format);
  result = vsnprintf(temp, targetSize, format, args);
  if (result != EOF)
  {
    if (strlen(temp) + strlen(target) > targetSize)
    {
      fprintf(stderr, "appendToStr: target buffer not large enough to hold additional string");
      return 0;
    }
    strcat(target, temp);
  }
  va_end(args);
  return result;
}

And you would use it like so:

char target[100] = {0};
...
appendToStr(target, sizeof target, "%s %d %f\n", "This is a test", 42, 3.14159);
appendToStr(target, sizeof target, "blah blah blah");

etc.

The function returns the value from vsprintf, which in most implementations is the number of bytes written to the destination. There are a few holes in this implementation, but it should give you some ideas.

John Bode
A: 

A snprintfcat() wrapper for snprintf():

size_t 
snprintfcat(
    char* buf,
    size_t bufSize,
    char const* fmt,
    ...)
{
    size_t result;
    va_list args;
    size_t len = strnlen( buf, bufSize);

    va_start( args, fmt);
    result = vsnprintf( buf + len, bufSize - len, fmt, args);
    va_end( args);

    return result + len;
}
Michael Burr