C字符串附加

时间:2011-05-05 16:33:36

标签: c string

我想附加两个字符串。我使用了以下命令:

new_str = strcat(str1, str2);

此命令更改str1的值。我希望new_str成为str1str2的连接,同时str1不得更改。

10 个答案:

答案 0 :(得分:59)

您还需要分配新空间。请考虑以下代码片段:

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?
}

您可能需要考虑更安全的strnlen(3)

已更新,见上文。在C运行时的某些版本中,malloc返回的内存未初始化为0.将new_str的第一个字节设置为零可确保它看起来像strcat的空字符串。

答案 1 :(得分:7)

执行以下操作:

strcat(new_str,str1);
strcat(new_str,str2);

答案 2 :(得分:1)

您必须先strncpy str1进入new_string

答案 3 :(得分:1)

我编写了一个函数支持动态变量字符串append,就像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;
}

答案 4 :(得分:0)

strcpy(str1+strlen(str1), str2);

答案 5 :(得分:0)

您可以使用asprintf将两者连接成一个新字符串:

char *new_str;
asprintf(&new_str,"%s%s",str1,str2);

答案 6 :(得分:0)

我需要附加子串来创建一个ssh命令,我用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);
}

答案 7 :(得分:0)

考虑使用伟大但未知的open_memstream()函数。

FILE *open_memstream(char **ptr, size_t *sizeloc);

使用示例:

// 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);

如果您事先并不知道要追加的内容的长度,这比自己管理缓冲区更方便,更安全。

答案 8 :(得分:-2)

strcat的man页面说arg1和arg2被附加到arg1 ..并返回s1的指针。如果您不想打扰str1,str2,那么您已经编写了自己的功能。

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;    
}

希望这能解决你的目的

答案 9 :(得分:-3)

您可以尝试这样的事情:

strncpy(new_str, str1, strlen(str1));
strcat(new_str, str2);

有关strncpy的更多信息:http://www.cplusplus.com/reference/clibrary/cstring/strncpy/