C string append

后端 未结 10 1264
野趣味
野趣味 2020-12-01 06:21

I want to append two strings. I used the following command:

new_str = strcat(str1, str2);

This command changes the value of str1

相关标签:
10条回答
  • 2020-12-01 06:35

    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;
    }
    
    0 讨论(0)
  • 2020-12-01 06:37

    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.

    0 讨论(0)
  • strcpy(str1+strlen(str1), str2);
    
    0 讨论(0)
  • 2020-12-01 06:38

    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.

    0 讨论(0)
  • 2020-12-01 06:41

    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/

    0 讨论(0)
  • 2020-12-01 06:42

    You could use asprintf to concatenate both into a new string:

    char *new_str;
    asprintf(&new_str,"%s%s",str1,str2);
    
    0 讨论(0)
提交回复
热议问题