How to add lines using sprintf?

I have a problem with sprintf.

Suppose my code snippet is:

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

A few hundred sprints ....

If I do this, it will be overwritten.

How can I avoid overwriting with sprintf. If I give printf at the end, I want to see all the lines.

+49
c printf
Apr 20 '10 at
source share
15 answers

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.

+89
Apr 20 2018-10-10T00:
source share
 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 error resistance. This is useful if you don't care when errors occur, as long as you can continue your fun journey 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")); 
+62
Apr 20 '10 at
source share

For security (buffer overflow) I recommend using 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");
+27
Apr 20 '10 at 11:45
source share

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; } 
+12
Apr 20 '10 at 17:10
source share

Why do you want to use sprintf to concatenate strings when there are methods designed specifically for what you need, like strcat and strncat ?

+6
Apr 20 '10 at 10:44
source share

Use the return value of sprintf()

 Buffer += sprintf(Buffer,"Hello World"); Buffer += sprintf(Buffer,"Good Morning"); Buffer += sprintf(Buffer,"Good Afternoon"); 
+6
Feb 18 '16 at 12:25
source share

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.

+3
Apr 20 '10 at 14:39
source share

I believe the following method works well.

 sprintf(Buffer,"Hello World"); sprintf(&Buffer[strlen[Buffer]],"Good Morning"); sprintf(&Buffer[strlen[Buffer]],"Good Afternoon"); 
+3
Mar 28 '13 at 13:45
source share

I think you are looking for fmemopen(3) :

 #include <assert.h> #include <stdio.h> int main(void) { char buf[128] = { 0 }; FILE *fp = fmemopen(buf, sizeof(buf), "w"); assert(fp); fprintf(fp, "Hello World!\n"); fprintf(fp, "%s also work, of course.\n", "Format specifiers"); fclose(fp); puts(buf); return 0; } 

If dynamic storage is more suitable for you, you can follow Liam's recommendations on using open_memstream(3) :

 #include <assert.h> #include <stdio.h> #include <stdlib.h> int main(void) { char *buf; size_t size; FILE *fp = open_memstream(&buf, &size); assert(fp); fprintf(fp, "Hello World!\n"); fprintf(fp, "%s also work, of course.\n", "Format specifiers"); fclose(fp); puts(buf); free(buf); return 0; } 
+3
Feb 17 '16 at 13:37
source share

You can use the simple line shown below to add lines to a single buffer:

 sprintf(Buffer,"%s %s %s","Hello World","Good Morning","Good Afternoon"); 
+1
May 18 '13 at 15:47
source share

Use strcat http://www.cplusplus.com/reference/cstring/strcat/

 int main () { char str[80]; strcpy (str,"these "); strcat (str,"strings "); strcat (str,"are "); strcat (str,"concatenated."); puts (str); return 0; } Output: these strings are concatenated. 
+1
Oct 29 '16 at 5:06 on
source share

I am writing a support line for supporting a dynamic variable, for example PHP str append: str. str .... etc.

 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> int str_append(char **json, const char *format, ...) { char *str = NULL; char *old_json = NULL, *new_json = NULL; va_list arg_ptr; va_start(arg_ptr, format); vasprintf(&str, format, arg_ptr); // save old json asprintf(&old_json, "%s", (*json == NULL ? "" : *json)); // calloc new json memory new_json = (char *)calloc(strlen(old_json) + strlen(str) + 1, sizeof(char)); strcat(new_json, old_json); strcat(new_json, str); if (*json) free(*json); *json = new_json; free(old_json); free(str); return 0; } int main(int argc, char *argv[]) { char *json = NULL; /* str_append(&json, "name: %d, %d, %d", 1, 2, 3); str_append(&json, "sex: %s", "male"); str_append(&json, "end"); str_append(&json, ""); str_append(&json, "{\"ret\":true}"); */ int i; for (i = 0; i < 100; i++) { str_append(&json, "id-%d", i); } printf("%s\n", json); if (json) free(json); return 0; } 
0
Aug 14 '17 at 10:09 on
source share

A small example with full code

Using only the stdio standard flat standard library

 #include <stdio.h> int main() { char c[1024]; int i=0; i+=sprintf(c+i,"We " ); i+=sprintf(c+i,"Love " ); sprintf(c+i,"Coding"); printf("%s",c); } 

EXIT: We love to code

0
Jul 30 '18 at 23:26
source share

Using strcat ( buffer , " Your new string...here ") as an option.

-2
Feb 17 '17 at 6:42
source share

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 consider possible buffered ovewflows!

-four
Apr 20 '10 at 10:44
source share



All Articles