我正在尝试复制包含“\0”的数据。我正在使用 C++ 。
当研究结果是否定的时,我决定编写自己的函数来将数据从一个 char* 复制到另一个 char*。但它没有返回想要的结果!
我的尝试如下:
#include <iostream>
char* my_strcpy( char* arr_out, char* arr_in, int bloc )
{
char* pc= arr_out;
for(size_t i=0;i<bloc;++i)
{
*arr_out++ = *arr_in++ ;
}
*arr_out = '\0';
return pc;
}
int main()
{
char * out= new char[20];
my_strcpy(out,"12345aa\0aaaaa AA",20);
std::cout<<"output data: "<< out << std::endl;
std::cout<< "the length of my output data: " << strlen(out)<<std::endl;
system("pause");
return 0;
}
the result is here:
我不明白我的代码有什么问题。
提前感谢您的帮助。
Your my_strcpy
工作正常,当你写一个char*
to cout
或计算它的长度strlen
they stop at \0
按照 C 字符串行为。顺便说一下,你可以使用memcpy
复制一块char
不管\0
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)