本文主要是介绍strcpy strcpy_s wcscpy wcscpy_s strncpy strncpy_s,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
strcpy & strcpy_s & wcscpy & wcscpy_s:将源字符串整体拷贝到目标地址,不支持部分拷贝?
wcscpy_s(dest, wcslen(src)+1, src); //src结束符也拷贝
strncpy & strncpy_s:支持部分拷贝字符串
from:https://blog.csdn.net/jiangdong2007/article/details/81297360
int _tmain(int argc, _TCHAR* argv[])
{
WCHAR ch[2]={0};
wcscpy_s(ch,2,TEXT("12")); //1、eroo 需要空间 要3个字节 + ‘\0’
WCHAR ch1[1]={0};
wcscpy_s(ch1,1,TEXT("12")); //2、erro 存储空间小于 src
//1 2两种情况都会报错的!!!
WCHAR ch2[3]={0};
wcscpy_s(ch2,3,TEXT("12")); //ok 此函数拷贝安全的,但是还是会抛出异常的!!
return 0;
}
The strcpy_s function copies the contents in the address of strSource, including the terminating null character, to the location specified by strDestination. The destination string must be large enough to hold the source string, including the terminating null character. The behavior of strcpy_s is undefined if the source and destination strings overlap.
wcscpy_s and _mbscpy_s are wide-character and multibyte-character versions of strcpy_s respectively. The arguments and return value of wcscpy_s are wide character strings; those of _mbscpy_s are multibyte character strings. These three functions behave identically otherwise.
If strDestination or strSource is a null pointer, or if the destination string is too small, the invalid parameter handler is invoked as described in Parameter Validation. If execution is allowed to continue, these functions return EINVAL and set errno to EINVAL.
Upon successful execution, the destination string will always be null terminated.
In C++, using these functions is simplified by template overloads; the overloads can infer buffer length automatically (eliminating the need to specify a size argument) and they can automatically replace older, non-secure functions with their newer, secure counterparts. For more information, see Secure Template Overloads.
The debug versions of these functions first fill the buffer with 0xFD. To disable this behavior, use _CrtSetDebugFillThreshold.
这篇关于strcpy strcpy_s wcscpy wcscpy_s strncpy strncpy_s的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!