简体   繁体   English

复制内部为空的字符串

[英]Copying a string with nulls inside

I want to copy a string in C (Windows) that contains nulls in it. 我想在C(Windows)中复制一个包含空值的字符串。 I need a function to which I will pass buffer length so that the NULL characters will be meaningless. 我需要一个将缓冲区长度传递给的函数,以便NULL字符毫无意义。 I found StringCbCopy function but it still stops at the first NULL character. 我找到了StringCbCopy函数,但它仍然在第一个NULL字符处停止。

由于您知道长度,因此请使用memcpy()

Here is a quick bit of code that may help: 以下是一些可能会有所帮助的代码:

char array1[5] = "test", array2[5];
int length = 5;
memcpy(array2, array1, length*sizeof(char));
//the sizeof() is redundant in this because each char is a byte long
//but it is useful if you are working with other datatypes

memcpy probably will become your best friend for situations like this. 在这种情况下,memcpy可能会成为您最好的朋友。

It should be very easy to write your own function to do this. 编写自己的函数来执行此操作应该非常容易。 If you know the length of the string, just create a char[] or char* with the specified length, and copy characters one by one. 如果知道字符串的长度,只需创建具有指定长度的char []或char *,然后一个一个地复制字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM