I want to append two strings. I used the following command:
new_str = strcat(str1, str2);
This command changes the value of str1. I want new_str to be the concatanation of str1 and str2 and at the same time str1 is not to be changed.
You need to allocate new space as well. Consider this code fragment:
char * new_str ;
if((new_str = malloc(strlen(str1)+strlen(str2)+1)) != NULL){
new_str[0] = '\0'; // ensures the memory is an empty string
strcat(new_str,str1);
strcat(new_str,str2);
} else {
fprintf(STDERR,"malloc failed!\n");
// exit?
}
You might want to consider strnlen(3) which is slightly safer.
Updated, see above. In some versions of the C runtime, the memory returned by malloc isn't initialized to 0. Setting the first byte of new_str to zero ensures that it looks like an empty string to strcat.
do the following:
strcat(new_str,str1);
strcat(new_str,str2);
Consider using the great but unknown open_memstream() function.
FILE *open_memstream(char **ptr, size_t *sizeloc);
Example of usage :
// open the stream
FILE *stream;
char *buf;
size_t len;
stream = open_memstream(&buf, &len);
// write what you want with fprintf() into the stream
fprintf(stream, "Hello");
fprintf(stream, " ");
fprintf(stream, "%s\n", "world");
// close the stream, the buffer is allocated and the size is set !
fclose(stream);
printf ("the result is '%s' (%d characters)\n", buf, len);
free(buf);
If you don't know in advance the length of what you want to append, this is convenient and safer than managing buffers yourself.
You'll have to strncpy str1 into new_string first then.
You could use asprintf to concatenate both into a new string:
char *new_str;
asprintf(&new_str,"%s%s",str1,str2);
I write a function support dynamic variable string append, like 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 < 10; i++) {
str_append(&json, "id-%d", i);
}
printf("%s\n", json);
if (json) free(json);
return 0;
}
I needed to append substrings to create an ssh command, I solved with sprintf (Visual Studio 2013)
char gStrSshCommand[SSH_COMMAND_MAX_LEN]; // declare ssh command string
strcpy(gStrSshCommand, ""); // empty string
void appendSshCommand(const char *substring) // append substring
{
sprintf(gStrSshCommand, "%s %s", gStrSshCommand, substring);
}
strcpy(str1+strlen(str1), str2);
man page of strcat says that arg1 and arg2 are appended to arg1.. and returns the pointer of s1. If you dont want disturb str1,str2 then you have write your own function.
char * my_strcat(const char * str1, const char * str2)
{
char * ret = malloc(strlen(str1)+strlen(str2));
if(ret!=NULL)
{
sprintf(ret, "%s%s", str1, str2);
return ret;
}
return NULL;
}
Hope this solves your purpose
You can try something like this:
strncpy(new_str, str1, strlen(str1));
strcat(new_str, str2);
More info on strncpy: http://www.cplusplus.com/reference/clibrary/cstring/strncpy/
Related
I'm trying to parse a string around an arbitrary index. In my simplest test program I could come up with I have an input string I read the input into and then do memcpy to parse the string.
For testing this I am typing in "this text" as input. readInput is a function where I just have it calling getline(&input, &size, stdnin) and return the input pointer.
int main(){
char *input;
input = readInput();
int parseAround = 4;
char *token1;
char *token2;
memcpy(token1, inputBuffer, 4);
printf("token: %s\n", token1); //prints "this"
memcpy(token1, inputBuffer + (parseAround+1), 4);
//when changed to memcpy(token2,...); segfaults
printf("token: %s\n", token1); //prints "text"
free(input);
return 0;
}
However when I change the second memcpy to use token2 rather than token1, I get a segmentation fault. Why is this?
You most likely need to allocate memory for token1.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char *input = NULL;
size_t len = 0;
ssize_t read;
/* Read in the line "Hello world" */
read = getline(&input, &len, stdin);
printf("Retrieved line of length %zu :\n", read);
printf("%s", input);
/* Allocate memory for both parts of the string */
char *token1 = malloc((read+1) * sizeof(char *));
char *token2 = malloc((read+1) * sizeof(char *));
memcpy(token1, input, 6);
printf("token: %s\n", token1); //prints "Hello"
memcpy(token2, (input+6), 5);
printf("token: %s\n", token2); //prints "world"
free(input);
return 0;
}
Read in the line, allocate memory for each string part, and then copy the part you want into your s
I have a simple program with two string which I supposed to concatenate in the called function and return to the calling function.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void call_func(char *str1,char *str2)
{
char *msg_concat;
asprintf(&msg_concat,"%s%s", str1, str2);
printf("%s",msg_concat);
str2 = msg_concat;
}
int main()
{
char *str1="new" ;
char *full_msg ="test";
call_func(str1,full_msg);
printf("value of full_msg=%s",full_msg);
return 0;
}
I have used asprintf() for concatenation but the value is not returned in the main function. As I have used the pointer, is not it
is expected to return the changed value??
Further while assigning the pointer , in my understanding reference is copied rather than the values. now here I need to free the
allocated memory msg_concat otherwise there will be memory leak . how am supposed to free memory if the function uses pointer and return the
reference to the calling function??
free(msg_concat) is not working while in the last line of called function
as-
void call_func(char *str1,char *str2)
{
char *msg_concat;
asprintf(&msg_concat,"%s%s", str1, str2);
printf("%s",msg_concat);
str2 = msg_concat;
free(msg_concat);
}
actually in my recent project, I have the scenerio that the calling function has
GSList *parsed_msg = LineParser(dev_ip,encoded_msg, "\n", file_path, unique_id);
and the called function is
GSList *LineParser(char* dev_ip, char *msg_full, char *separator, char *file_path, int unique_id)
{
GSList *parsed_msg = NULL;
char connection_id[50];
sprintf(connection_id,"%s|%d", dev_ip, unique_id);
char *msg_concat;
// inserting {file_path : last_not_complete_line} logic to TCP_CACHE
//and removing the correspoing last_line
g_mutex_lock (&mutex_hash_main);
// char *last_line = (char *) (g_hash_table_lookup((GHashTable *) g_hash_table_lookup(TCP_CACHE, connection_id), file_path));
GHashTable *t_filepath_msg_dict = NULL; //for {file_path : last_not_complete_line}
if (TCP_CACHE != NULL)
{
t_filepath_msg_dict = (GHashTable *)(g_hash_table_lookup(TCP_CACHE, connection_id));
if (t_filepath_msg_dict != NULL)
{
char *last_line = (char *) (g_hash_table_lookup(t_filepath_msg_dict, file_path));
if(last_line != NULL) //if the hash has device ip, append the value to msg
{
zlog_debug(c,"concatenating: str1: %s and str2: %s\n", last_line, msg_full);
asprintf(&msg_concat,"%s%s", last_line, msg_full);
//msg_concat = concat(last_line,msg_full);
g_hash_table_remove(t_filepath_msg_dict, file_path);
msg_full = msg_concat;
}
}
}
int msg_len = strlen(msg_full);
char last_char = msg_full[msg_len - 1];
zlog_debug(c, "len of message: %d", msg_len);
zlog_debug(c, "last char is : %c", last_char);
char *token=NULL;
char *remaining_str=NULL;
token = strtok_r(msg_full, "\n", &remaining_str);
while(token != NULL)
{
if(token[0]==' ')
{
token = trimwhitespace_parser (token);
if(strcmp(token,"")==0)
{
//insert this token to GSList
parsed_msg = g_slist_prepend (parsed_msg, token);
token = strtok_r(NULL, "\n", &remaining_str);
continue;
}
}
if(strcmp(remaining_str,"")==0)
{
if(strlen(token) > 10000)
{
zlog_warn(c, "Message too big(more than 10000 len). Stop looking for new line and process msg");
g_hash_table_remove(t_filepath_msg_dict, file_path);
}
else
{
if(last_char=='\n')
{
//new line is the last character. do nothing
zlog_debug(c, "last character is new line");
}
else
{
zlog_debug(c, "last character is not new line");
//new line not received
if (t_filepath_msg_dict == NULL) //insert new record
{
GHashTable *each_filepath_msg_dict = g_hash_table_new_full(g_str_hash, g_str_equal, key_str_destroy_cb_parser, value_str_destroy_cb_parser);
zlog_debug(c,"Inserting file_path: %s to connection_id: %s", file_path, connection_id);
g_hash_table_insert(each_filepath_msg_dict, strdup(file_path), strdup(token));
g_hash_table_insert(TCP_CACHE, strdup(connection_id), each_filepath_msg_dict);
}
else //update existing record
{
zlog_debug(c,"Connection_id :%s is already found; appending/replacing file_path :%s", connection_id, file_path);
g_hash_table_insert(t_filepath_msg_dict, strdup(file_path), strdup(token));
}
g_mutex_unlock(&mutex_hash_main);
return parsed_msg;
}
}
}
//insert token to GSList
parsed_msg = g_slist_prepend (parsed_msg, token);
token = strtok_r(NULL, "\n", &remaining_str);
}
g_mutex_unlock(&mutex_hash_main);
return parsed_msg;
}
I have seen that i have problem with memory leak as i have to free msg_concat,as in the given answer it is said that the changed value doesnot return to calling function. where is the appropriate place to free the asprintf msg_concat pointer???
Pointers are passed by value, so the last assignment has no effect in the caller. There are two ways to do what you wish:
Pass a pointer to pointer for str2, or
Return a new string to the caller.
Returning the result has a slightly more explicit appearance to the callers of your function, but the pointer-to-pointer approach is valid as well.
An additional issue that you need to solve is freeing the pointer passed into your function. You cannot pass a string literal if you would like to free the value:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
char* call_func(char *str1,char *str2) {
char *msg_concat = NULL;
asprintf(&msg_concat,"%s%s", str1, str2);
printf("%s",msg_concat);
free(str2);
return msg_concat;
}
int main() {
char *str1="new" ;
char *full_msg =strdup("test"); // Put the string in dynamic memory
full_msg = call_func(str1, full_msg);
printf("value of full_msg=%s", full_msg);
free(full_msg);
return 0;
}
As C uses pass-by-value for function parameter passing, in your code
str2 = msg_concat;
is pretty much useless, in regard to returning the value to main(). Instead, you may want to use strcpy() to copy the content to the memory area pointed by str2, which will reflect in main().
Also, after you have copied the content, you can free() the msg_concat before the end of call_func().
Now, even this won't solve your issue, as you're calling call_func() with a second argument as a string literal. This is illegal in two ways
modification of string literal will cause UB.
the destination string (as used in strcpy()) won't have enough memory to hold the final output.
Solution: A possible way to solve this can be
Change the type of full_msg to a fixed length array, which has enough length to hold the concatenated output, like
char full_msg[32] = "test";
then, with a call like
call_func(str1,full_msg);
and inside, with a strcpy(str2, msg_concat);, you can achieve your goal.
Additionally, you can use free(msg_concat); also.
I have the following code:
#include <stdio.h>
#include <string.h>
int main (void) {
char str[] = "John|Doe|Melbourne|6270|AU";
char fname[32], lname[32], city[32], zip[32], country[32];
char *oldstr = str;
strcpy(fname, strtok(str, "|"));
strcpy(lname, strtok(NULL, "|"));
strcpy(city, strtok(NULL, "|"));
strcpy(zip, strtok(NULL, "|"));
strcpy(country, strtok(NULL, "|"));
printf("Firstname: %s\n", fname);
printf("Lastname: %s\n", lname);
printf("City: %s\n", city);
printf("Zip: %s\n", zip);
printf("Country: %s\n", country);
printf("STR: %s\n", str);
printf("OLDSTR: %s\n", oldstr);
return 0;
}
Execution output:
$ ./str
Firstname: John
Lastname: Doe
City: Melbourne
Zip: 6270
Country: AU
STR: John
OLDSTR: John
Why can't I keep the old data nor in the str or oldstr, what am I doing wrong and how can I not alter the data or keep it?
when you do strtok(NULL, "|") strtok() find token and put null on place (replace token with \0) and modify string.
you str, becomes:
char str[] = John0Doe0Melbourne062700AU;
Str array in memory
+------------------------------------------------------------------------------------------------+
|'J'|'o'|'h'|'n'|0|'D'|'o'|'e'|0|'M'|'e'|'l'|'b'|'o'|'u'|'r'|'n'|'e'|0|'6'|'2'|'7'|'0'|0|'A'|'U'|0|
+------------------------------------------------------------------------------------------------+
^ replace | with \0 (ASCII value is 0)
Consider the diagram is important because char '0' and 0 are diffident (in string 6270 are char in figure parenthesised by ' where for \0 0 is as number)
when you print str using %s it print chars upto first \0 that is John
To keep your original str unchanged you should fist copy str into some tempstr variable and then use that tempstr string in strtok():
char str[] = "John|Doe|Melbourne|6270|AU";
char* tempstr = calloc(strlen(str)+1, sizeof(char));
strcpy(tempstr, str);
Now use this tempstr string in place of str in your code.
Because oldstr is just a pointer, an assignment will not make a new copy of your string.
Copy it before passing str to the strtok:
char *oldstr=malloc(sizeof(str));
strcpy(oldstr,str);
Your corrected version:
#include <stdio.h>
#include <string.h>
#include<malloc.h>
int main (void) {
char str[] = "John|Doe|Melbourne|6270|AU";
char fname[32], lname[32], city[32], zip[32], country[32];
char *oldstr = malloc(sizeof(str));
strcpy(oldstr,str);
...................
free(oldstr);
return 0;
}
EDIT:
As #CodeClown mentioned, in your case, it's better to use strncpy. And instead of fixing the sizes of fname etc before hand, you can have pointers in their place and allocate the memory as is required not more and not less. That way you can avoid writing to the buffer out of bounds......
Another Idea:
would be to assign the result of strtok to pointers *fname, *lname, etc.. instead of arrays. It seems the strtok is designed to be used that way after seeing the accepted answer.
Caution:In this way, if you change str further that would be reflected in fname,lname also. Because, they just point to str data but not to new memory blocks. So, use oldstr for other manipulations.
#include <stdio.h>
#include <string.h>
#include<malloc.h>
int main (void) {
char str[] = "John|Doe|Melbourne|6270|AU";
char *fname, *lname, *city, *zip, *country;
char *oldstr = malloc(sizeof(str));
strcpy(oldstr,str);
fname=strtok(str,"|");
lname=strtok(NULL,"|");
city=strtok(NULL, "|");
zip=strtok(NULL, "|");
country=strtok(NULL, "|");
printf("Firstname: %s\n", fname);
printf("Lastname: %s\n", lname);
printf("City: %s\n", city);
printf("Zip: %s\n", zip);
printf("Country: %s\n", country);
printf("STR: %s\n", str);
printf("OLDSTR: %s\n", oldstr);
free(oldstr);
return 0;
}
strtok requires an writeable input string and it modifies the input string. If you want to keep the input string you have to a make a copy of it first.
For example:
char str[] = "John|Doe|Melbourne|6270|AU";
char oldstr[32];
strcpy(oldstr, str); // Use strncpy if you don't know
// the size of str
You just copy the pointer to the string, but not the string itself. Use strncpy() to create a copy.
char *oldstr = str; // just copy of the address not the string itself!
The for() loop below shows how code calls strtok() at only one location.
int separate( char *flds[], int size, char *fullStr ) {
int count = 0;
for( char *cp = fullStr; ( cp = strtok( cp, " " ) ) != NULL; cp = NULL ) {
flds[ count ] = strdup( cp ); // must be free'd later!
if( ++count == size )
break;
}
return( count );
}
How do I add two strings?
I tried name = "derp" + "herp";, but I got an error:
Expression must have integral or enum type
C does not have the support for strings that some other languages have. A string in C is just a pointer to an array of char that is terminated by the first null character. There is no string concatenation operator in C.
Use strcat to concatenate two strings. You could use the following function to do it:
#include <stdlib.h>
#include <string.h>
char* concat(const char *s1, const char *s2)
{
char *result = malloc(strlen(s1) + strlen(s2) + 1); // +1 for the null-terminator
// in real code you would check for errors in malloc here
strcpy(result, s1);
strcat(result, s2);
return result;
}
This is not the fastest way to do this, but you shouldn't be worrying about that now. Note that the function returns a block of heap allocated memory to the caller and passes on ownership of that memory. It is the responsibility of the caller to free the memory when it is no longer needed.
Call the function like this:
char* s = concat("derp", "herp");
// do things with s
free(s); // deallocate the string
If you did happen to be bothered by performance then you would want to avoid repeatedly scanning the input buffers looking for the null-terminator.
char* concat(const char *s1, const char *s2)
{
const size_t len1 = strlen(s1);
const size_t len2 = strlen(s2);
char *result = malloc(len1 + len2 + 1); // +1 for the null-terminator
// in real code you would check for errors in malloc here
memcpy(result, s1, len1);
memcpy(result + len1, s2, len2 + 1); // +1 to copy the null-terminator
return result;
}
If you are planning to do a lot of work with strings then you may be better off using a different language that has first class support for strings.
#include <stdio.h>
int main(){
char name[] = "derp" "herp";
printf("\"%s\"\n", name);//"derpherp"
return 0;
}
David Heffernan explained the issue in his answer, and I wrote the improved code. See below.
A generic function
We can write a useful variadic function to concatenate any number of strings:
#include <stdlib.h> // calloc
#include <stdarg.h> // va_*
#include <string.h> // strlen, strcpy
char* concat(int count, ...)
{
va_list ap;
int i;
// Find required length to store merged string
int len = 1; // room for NULL
va_start(ap, count);
for(i=0 ; i<count ; i++)
len += strlen(va_arg(ap, char*));
va_end(ap);
// Allocate memory to concat strings
char *merged = calloc(sizeof(char),len);
int null_pos = 0;
// Actually concatenate strings
va_start(ap, count);
for(i=0 ; i<count ; i++)
{
char *s = va_arg(ap, char*);
strcpy(merged+null_pos, s);
null_pos += strlen(s);
}
va_end(ap);
return merged;
}
Usage
#include <stdio.h> // printf
void println(char *line)
{
printf("%s\n", line);
}
int main(int argc, char* argv[])
{
char *str;
str = concat(0); println(str); free(str);
str = concat(1,"a"); println(str); free(str);
str = concat(2,"a","b"); println(str); free(str);
str = concat(3,"a","b","c"); println(str); free(str);
return 0;
}
Output:
// Empty line
a
ab
abc
Clean-up
Note that you should free up the allocated memory when it becomes unneeded to avoid memory leaks:
char *str = concat(2,"a","b");
println(str);
free(str);
I'll assume you need it for one-off things. I'll assume you're a PC developer.
Use the Stack, Luke. Use it everywhere. Don't use malloc / free for small allocations, ever.
#include <string.h>
#include <stdio.h>
#define STR_SIZE 10000
int main()
{
char s1[] = "oppa";
char s2[] = "gangnam";
char s3[] = "style";
{
char result[STR_SIZE] = {0};
snprintf(result, sizeof(result), "%s %s %s", s1, s2, s3);
printf("%s\n", result);
}
}
If 10 KB per string won't be enough, add a zero to the size and don't bother, - they'll release their stack memory at the end of the scopes anyway.
You should use strcat, or better, strncat. Google it (the keyword is "concatenating").
You cannot add string literals like that in C. You have to create a buffer of size of string literal one + string literal two + a byte for null termination character and copy the corresponding literals to that buffer and also make sure that it is null terminated. Or you can use library functions like strcat.
Concatenate Strings
Concatenating any two strings in C can be done in atleast 3 ways :-
1) By copying string 2 to the end of string 1
#include <stdio.h>
#include <string.h>
#define MAX 100
int main()
{
char str1[MAX],str2[MAX];
int i,j=0;
printf("Input string 1: ");
gets(str1);
printf("\nInput string 2: ");
gets(str2);
for(i=strlen(str1);str2[j]!='\0';i++) //Copying string 2 to the end of string 1
{
str1[i]=str2[j];
j++;
}
str1[i]='\0';
printf("\nConcatenated string: ");
puts(str1);
return 0;
}
2) By copying string 1 and string 2 to string 3
#include <stdio.h>
#include <string.h>
#define MAX 100
int main()
{
char str1[MAX],str2[MAX],str3[MAX];
int i,j=0,count=0;
printf("Input string 1: ");
gets(str1);
printf("\nInput string 2: ");
gets(str2);
for(i=0;str1[i]!='\0';i++) //Copying string 1 to string 3
{
str3[i]=str1[i];
count++;
}
for(i=count;str2[j]!='\0';i++) //Copying string 2 to the end of string 3
{
str3[i]=str2[j];
j++;
}
str3[i]='\0';
printf("\nConcatenated string : ");
puts(str3);
return 0;
}
3) By using strcat() function
#include <stdio.h>
#include <string.h>
#define MAX 100
int main()
{
char str1[MAX],str2[MAX];
printf("Input string 1: ");
gets(str1);
printf("\nInput string 2: ");
gets(str2);
strcat(str1,str2); //strcat() function
printf("\nConcatenated string : ");
puts(str1);
return 0;
}
Without GNU extension:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
const char str1[] = "First";
const char str2[] = "Second";
char *res;
res = malloc(strlen(str1) + strlen(str2) + 1);
if (!res) {
fprintf(stderr, "malloc() failed: insufficient memory!\n");
return EXIT_FAILURE;
}
strcpy(res, str1);
strcat(res, str2);
printf("Result: '%s'\n", res);
free(res);
return EXIT_SUCCESS;
}
Alternatively with GNU extension:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
const char str1[] = "First";
const char str2[] = "Second";
char *res;
if (-1 == asprintf(&res, "%s%s", str1, str2)) {
fprintf(stderr, "asprintf() failed: insufficient memory!\n");
return EXIT_FAILURE;
}
printf("Result: '%s'\n", res);
free(res);
return EXIT_SUCCESS;
}
See malloc, free and asprintf for more details.
#include <string.h>
#include <stdio.h>
int main()
{
int a,l;
char str[50],str1[50],str3[100];
printf("\nEnter a string: ");
scanf("%s",str);
str3[0]='\0';
printf("\nEnter the string which you want to concat with string one: ");
scanf("%s",str1);
strcat(str3,str);
strcat(str3,str1);
printf("\nThe string is %s\n",str3);
}
using memcpy
char *str1="hello";
char *str2=" world";
char *str3;
str3=(char *) malloc (11 *sizeof(char));
memcpy(str3,str1,5);
memcpy(str3+strlen(str1),str2,6);
printf("%s + %s = %s",str1,str2,str3);
free(str3);
my here use asprintf
sample code:
char* fileTypeToStr(mode_t mode) {
char * fileStrBuf = NULL;
asprintf(&fileStrBuf, "%s", "");
bool isFifo = (bool)S_ISFIFO(mode);
if (isFifo){
asprintf(&fileStrBuf, "%s %s,", fileStrBuf, "FIFO");
}
...
bool isSocket = (bool)S_ISSOCK(mode);
if (isSocket){
asprintf(&fileStrBuf, "%s %s,", fileStrBuf, "Socket");
}
return fileStrBuf;
}
In C, you don't really have strings, as a generic first-class object. You have to manage them as arrays of characters, which mean that you have to determine how you would like to manage your arrays. One way is to normal variables, e.g. placed on the stack. Another way is to allocate them dynamically using malloc.
Once you have that sorted, you can copy the content of one array to another, to concatenate two strings using strcpy or strcat.
Having said that, C do have the concept of "string literals", which are strings known at compile time. When used, they will be a character array placed in read-only memory. It is, however, possible to concatenate two string literals by writing them next to each other, as in "foo" "bar", which will create the string literal "foobar".
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, it's getting overwritten.
How can I avoid overwriting using sprintf? If I give a printf at the end I want to see all the lines.
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.
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"));
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");
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;
}
Use the return value of sprintf()
Buffer += sprintf(Buffer,"Hello World");
Buffer += sprintf(Buffer,"Good Morning");
Buffer += sprintf(Buffer,"Good Afternoon");
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?
Small full code example
Using flat plain stdio standard library only
#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);
}
OUTPUT: We Love Coding
I find the following method works nicely.
sprintf(Buffer,"Hello World");
sprintf(&Buffer[strlen(Buffer)],"Good Morning");
sprintf(&Buffer[strlen(Buffer)],"Good Afternoon");
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 use-case you could follow Liam's excellent suggestion about 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;
}
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.
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.
You can use the simple line shown below to append strings in one buffer:
sprintf(Buffer,"%s %s %s","Hello World","Good Morning","Good Afternoon");
char string1[] = "test";
char string2[] = "string";
int len = sizeof(string1) + sizeof(string2);
char totalString[len];
sprintf(totalString, "%s%s",string1,string2);
I write a function support dynamic variable string append, like 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;
}
Using strcat(buffer,"Your new string...here"), as an option.
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!