memcpy 不是实际复制

memcpy not actual copying

    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

字符串的其余部分怎么办?我希望缓冲区包含所有这些字符数组...

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

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

的东西

--this-is-a-boundary\n[=11=]

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

--this-is-a-boundary\n[=12=]Content-Disposition: form-data; name=\"metadata\"\n[=12=]

由于字符串以 \0 结尾,因此字符串仍然是第一个 \0 之前的部分。

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

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

这会在您追加下一个字符串时覆盖 \0。

    /*Always rember to remove the null terminatior [=10=] 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 [=10=] 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 [=10=] 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