我想要连接两个字符串。我使用了以下命令:
new_str = strcat(str1, str2);
这个命令改变了str1
的值。我想让new_str
是str1
和str2
连接起来的结果,同时str1
的值不会被改变。
你需要分配新的空间。考虑以下代码片段:
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
使用。
malloc
可能会返回垃圾值,即使在现代系统上也是如此。calloc
则提供了经过清零的内存空间。 - Kornelvoid *
引用,本质上什么也不是--根据定义。 - Charlie Martin请执行以下操作:
strcat(new_str,str1);
strcat(new_str,str2);
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);
open_memstream
是 POSIX 的一部分,而不是 ISO C 标准。因此,在非 Unix 系统(如 Windows)上不可用,甚至在所有 Unix 系统上也不可用。strcat
是一个更具可移植性的解决方案。 - Denis Washingtonstrncpy
将 str1
复制到 new_string
中。asprintf
将它们连接成一个新的字符串:char *new_str;
asprintf(&new_str,"%s%s",str1,str2);
asnprintf
可用。它不是“标准”的。 - László Papp#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;
}
我需要添加子字符串来创建一个ssh命令,我用(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);
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;
}
希望这能解决你的问题
strncpy(new_str, str1, strlen(str1));
strcat(new_str, str2);
有关strncpy的更多信息:http://www.cplusplus.com/reference/clibrary/cstring/strncpy/
strncpy
函数的n
参数的严重误解,但你需要提供new_str
的缓冲区大小,而不是str1
的长度。如果new_str
只能容纳5个字符,但str1
却有1000个字符,那么会发生什么呢? - indiv
str1
、str2
和new_str
的大小,并使用strncat
而不仅仅是strcat
。这将有助于避免一些缓冲区溢出错误。 - Brendan Long