Ik wil twee strings toevoegen. Ik heb het volgende commando gebruikt:
new_str = strcat(str1, str2);
Dit commando wijzigt de waarde van str1
. Ik wil dat new_str
de aaneenschakeling is van str1
en str2
en dat tegelijkertijd str1
niet wordt gewijzigd .
Antwoord 1, autoriteit 100%
Je moet ook nieuwe ruimte toewijzen. Overweeg dit codefragment:
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?
}
Misschien wil je strnlen(3)
overwegen, wat iets veiliger is.
Bijgewerkt, zie hierboven. In sommige versies van de C-runtime wordt het geheugen dat wordt geretourneerd door malloc
niet geïnitialiseerd op 0. Door de eerste byte van new_str
in te stellen op nul zorgt ervoor dat het eruitziet als een lege tekenreeks naar strcat.
Antwoord 2, autoriteit 14%
doe het volgende:
strcat(new_str,str1);
strcat(new_str,str2);
Antwoord 3, autoriteit 9%
Overweeg om de geweldige, maar onbekende functie open_memstream() te gebruiken.
FILE *open_memstream(char **ptr, size_t *sizeloc);
Voorbeeld van gebruik:
// 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);
Als u van tevoren niet weet, is dit de lengte van wat u wilt toevoegen, dit is handig en veiliger dan het beheren van buffers zelf.
Antwoord 4
Ik schrijf een functie Ondersteuning Dynamische variabele string toevoegen, zoals PHP Str-appaan: Str + Str + … enz.
#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;
}
Antwoord 5
U moet strncpy
str1
IN new_string
EERST DAN.
Antwoord 6
Je zou asprintf
kunnen gebruiken om beide samen te voegen tot een nieuwe string:
char *new_str;
asprintf(&new_str,"%s%s",str1,str2);
Antwoord 7
Ik moest substrings toevoegen om een ssh-commando te maken, dat heb ik opgelost met 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);
}
Antwoord 8
strcpy(str1+strlen(str1), str2);
Antwoord 9
man-pagina van strcat zegt dat arg1 en arg2 worden toegevoegd aan arg1.. en geeft de pointer van s1 terug. Als je str1,str2 niet wilt storen, moet je je eigen functie schrijven.
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;
}
Ik hoop dat dit je doel oplost
Antwoord 10
Je kunt zoiets als dit proberen:
strncpy(new_str, str1, strlen(str1));
strcat(new_str, str2);
Meer informatie over strncpy: http://www.cplusplus.com/reference/ library/cstring/strncpy/