Are you just adding string literals? Or are you going to add various data types (ints, float, etc.)?
It might be easier to divert this attention to its own function (the following assumption 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 will use it like this:
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");
and etc.
The function returns a value from vsprintf , which in most implementations is the number of bytes written to the destination. There are several phenomena in this implementation, but this should give you some ideas.
John Bode Apr 20 '10 at 14:39 2010-04-20 14:39
source share