【问题标题】:memcpy not actual copyingmemcpy 不是实际复制
【发布时间】:2017-07-19 18:32:24
【问题描述】:
    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数组?

【问题讨论】:

  • "是因为我复制了以NULL结尾的字符数组吗?"是的
  • 从传递给 memcpy 的长度中减去 1(最后一次调用除外)?
  • std::memcpy() 不是 C
  • @user3222184 帖子被标记为Cstd::memcpy() 不是有效的 C 代码。您可以选择用 C 或其他语言编写代码。
  • 字符串用strcpy 复制而不是memcpy。然后你可以用strcat连接其他字符串而不是计算起始位置:strcpy( buffer, boundary ); strcat( buffer, header1_a );

标签: c char memcpy c-strings


【解决方案1】:

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

--这是一个边界\n\0

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

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

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

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

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

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

【讨论】:

  • 没错。可以添加 strlen 和 sizeof 不返回相同的值:strlen(boundary)=21 而 sizeof(boundary)=22。
  • 使用strcat() 获得更简洁、更易读的程序。编译器可能会生成同样高效的代码。
【解决方案2】:
    /*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

【讨论】:

  • 请添加一些代码,以便其他用户可以测试您所描述的内容。谢谢!
  • @IgnacioAra 我希望这会有所启发
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-05
  • 1970-01-01
  • 1970-01-01
  • 2012-02-09
  • 1970-01-01
  • 2015-12-05
  • 2011-07-19
相关资源
最近更新 更多