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

But the output is :

--this-is-a-boundary

What happen to the rest of the string? I expect buffer contains all of the these character arrays...

Is it because of the fact that I have copied NULL-terminated char array?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
user3222184
  • 1,071
  • 2
  • 11
  • 28
  • I expect buffer contains `boundary` + `header1_a` + `header1_b` – user3222184 Jul 19 '17 at 18:34
  • 3
    "Is it because of the fact that I have copied NULL-terminated char array?" Yes – Justin Jul 19 '17 at 18:34
  • How do I not copying the NULL at the end of the character array? – user3222184 Jul 19 '17 at 18:35
  • 1
    Subtract 1 from length passed to memcpy (except in last call)? – HolyBlackCat Jul 19 '17 at 18:36
  • 7
    `std::memcpy()` is not C – chux - Reinstate Monica Jul 19 '17 at 18:37
  • Possible duplicate of [How do I concatenate two strings in C?](https://stackoverflow.com/questions/8465006/how-do-i-concatenate-two-strings-in-c) – Justin Jul 19 '17 at 18:39
  • I dont undertand now. @chux said std::memcpy() is not C. Should I use the C version? – user3222184 Jul 19 '17 at 18:40
  • **printf** will print character-by-character till it hits a `\0` null-terminator, **memcpy** copied all the strings including their null-terminators, what you need to do is to avoid copying the null-terminators. – Khaled.K Jul 19 '17 at 18:40
  • 1
    @user3222184 Post is tagged `C`, `std::memcpy()` is not valid C code. Your choice if to code in C or some other language. – chux - Reinstate Monica Jul 19 '17 at 18:41
  • Please post the [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) that shows the problem in your language of choice, and tag appropriately. The code shown is not C. You should have enough rep and 3+ years to know that. – Weather Vane Jul 19 '17 at 18:41
  • 1
    Strings are copied with `strcpy` not with `memcpy`. Then you can concatenate other strings with `strcat` instead of calculation of start position: `strcpy( buffer, boundary ); strcat( buffer, header1_a );` – i486 Jul 19 '17 at 18:43

2 Answers2

7

When you copy the first string, you get something like

--this-is-a-boundary\n\0

Then you copy the next string. You get

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

Since strings are \0-terminated, the string is still the part up to the first \0.

I think, it is quite clear, what you have to do …:

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

This overwrites the \0, when you append the next string.

Khaled.K
  • 5,828
  • 1
  • 33
  • 51
Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
  • That's correct. One could add that strlen and sizeof do not return the same value: strlen(boundary)=21 whereas sizeof(boundary)=22. – user803422 Jul 19 '17 at 19:43
  • 1
    Use `strcat()` for a cleaner and more readable program. The compiler will likely generate the same efficient code. – chqrlie Jul 19 '17 at 20:38
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
Charles
  • 1,844
  • 1
  • 14
  • 21