memcpy不是实际的复制

时间:2017-07-19 18:32:25

标签: c char memcpy c-strings

    char buffer[2000];

    char boundary[]= "--this-is-a-boundary\n";
    char header1_a[]= "Content-Disposition: form-data; name=\"metadata\"\n";
    char header1_b[]= "Content-Type: application/json; charset=UTF-8\n\n";
    printf("%s%s%s\n\n\n\n", boundary, header1_a, header1_b);
    std::memcpy(buffer, boundary, sizeof boundary);
    std::memcpy(buffer + sizeof boundary, header1_a, sizeof header1_a);
    std::memcpy(buffer + sizeof boundary + sizeof header1_a, header1_b, sizeof header1_b);
    std::memcpy(buffer + sizeof boundary + sizeof header1_a + sizeof header1_b,
                strJSONout, sizeof strJSONout);
    printf("%s", buffer);

但输出是:

--this-is-a-boundary

字符串的其余部分会发生什么?我希望buffer包含所有这些字符数组......

是不是因为我复制了以NULL结尾的char数组?

2 个答案:

答案 0 :(得分:7)

当您复制第一个字符串时,您会得到类似

的内容
  

- 这-is-a的边界\ n \ 0

然后复制下一个字符串。你得到了

  

- 这是一个边界\ n \ 0Content-Disposition:form-data;命名= \"元数据\" \ n \ 0

由于字符串是\ 0终止的,因此字符串仍然是第一个\ 0的部分。

我想,很清楚,你要做什么......:

std::memcpy(buffer + sizeof boundary - 1, header1_a, sizeof header1_a);

当你追加下一个字符串时,这会覆盖\ 0。

答案 1 :(得分:0)

    /*Always rember to remove the null terminatior \0 if this is not done it starts behaving abnormally the best way is to allocate memory and then copy or if not just use strcat it produces the same result

Below is a example simple to illustrate this */

 //Always rember to remove the null terminatior \0 if this is not done it starts //behaving abnormally the best way is to allocate memory and then copy or if not //just use strcat it produces the same result

//Below is a example simple to illustrate this

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(){


 char* buffer;

    char* boundary= "--this-is-a-boundary\\n";
    char* header1_= "Content-Disposition: form-data; name=\\\"metadata\\\"\\n";
    char* header1_b= "Content-Type: application/json; charset=UTF-8\\n\\n";
//first allocate memory
int n=strlen(boundary)+strlen(header1_)+strlen(header1_b);
char* str=malloc(n*sizeof(char));
//After allocating this amount memory use strncat to concatenate to form
//one string

char* str1=strncat(str,boundary,strlen(boundary));
char* str2=strncat(str1,header1_,strlen(header1_));
char* str3=strncat(str2,header1_b,strlen(header1_b));

printf("%s",str3);






}

//The out put will be as follows

//--this-is-a-boundary\nContent-Disposition: form-data; //name=\"metadata\"\nContent-Type: application/json; charset=UTF-8\n\n
//Process returned 0 (0x0)   execution time : 0.067 s
//Press any key to continue.

//NOTE I CHOOSE TO PRINT THE " AS \" AND THE NEW LINE \\n


THE CODE BELOW IS PRINTING NORMALLY WITH OUT THE \" AND THE NEW LINE \\n

//Always rember to remove the null terminatior \0 if this is not done it starts //behaving abnormally the best way is to allocate memory and then copy or if not //just use strcat it produces the same result

//Below is a example simple to illustrate this

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(){


 char* buffer;

    char* boundary= "--this-is-a-boundary\n";
    char* header1_= "Content-Disposition: form-data; name=\"metadata\"\n";
    char* header1_b= "Content-Type: application/json; charset=UTF-8\\n\n";
//first allocate memory
int n=strlen(boundary)+strlen(header1_)+strlen(header1_b);
char* str=malloc(n*sizeof(char));
//After allocating this amount memory use strncat to concatenate to form
//one string

char* str1=strncat(str,boundary,strlen(boundary));
char* str2=strncat(str1,header1_,strlen(header1_));
char* str3=strncat(str2,header1_b,strlen(header1_b));

printf("%s",str3);



}

//BELOW IS THE OUTPUT GENERATED BY THE ABOVE CODE

//--this-is-a-boundary
//Content-Disposition: form-data; name="metadata"
//Content-Type: application/json; charset=UTF-8\n

//Process returned 0 (0x0)   execution time : 0.071 s
//Press any key to continue.

//I hope this will help you