strcpy_s、wcscpy_s、_mbscpy_sstrcpy_s, wcscpy_s, _mbscpy_s

复制字符串。Copies a string. CRT 中的安全性增强功能所述,这些版本的 strcpy、wcscpy、_mbscpy 具有安全性增强功能。These versions of strcpy, wcscpy, _mbscpy have security enhancements, as described in Security Features in the CRT.


_mbscpy_s 无法用于在 Windows 运行时中执行的应用程序。_mbscpy_s cannot be used in applications that execute in the Windows Runtime. 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数For more information, see CRT functions not supported in Universal Windows Platform apps.


errno_t strcpy_s(  
   char *strDestination,  
   size_t numberOfElements,  
   const char *strSource   
errno_t wcscpy_s(  
   wchar_t *strDestination,  
   size_t numberOfElements,  
   const wchar_t *strSource   
errno_t _mbscpy_s(  
   unsigned char *strDestination,  
   size_t numberOfElements,  
   const unsigned char *strSource   
template <size_t size>  
errno_t strcpy_s(  
   char (&strDestination)[size],  
   const char *strSource   
); // C++ only  
template <size_t size>  
errno_t wcscpy_s(  
   wchar_t (&strDestination)[size],  
   const wchar_t *strSource   
); // C++ only  
template <size_t size>  
errno_t _mbscpy_s(  
   unsigned char (&strDestination)[size],  
   const unsigned char *strSource   
); // C++ only  


目标字符串缓冲区的位置。Location of the destination string buffer.

多字节窄函数 char 单元以及宽函数 wchar_t 单元中的目标字符串缓冲区的大小。Size of the destination string buffer in char units for narrow and multi-byte functions, and wchar_t units for wide functions.

以 null 结尾的源字符串缓冲区。Null-terminated source string buffer.

返回值Return Value

如果成功,则为零;否则返回错误。Zero if successful; otherwise, an error.

错误条件Error Conditions

strDestination numberOfElements strSource 返回值Return value strDestination 的内容Contents of strDestination
NULL 任何any 任何any EINVAL 未修改not modified
任何any 任何any NULL EINVAL strDestination[0] 设置为 0strDestination[0] set to 0
任何any 0 或过小0, or too small 任何any ERANGE strDestination[0] 设置为 0strDestination[0] set to 0


strcpy_s 函数将 strSource 地址中的内容(包括结尾的 null 字符)复制到 strDestination 指定的位置。The strcpy_s function copies the contents in the address of strSource, including the terminating null character, to the location that's specified by strDestination. 目标字符串必须足够大以保存源字符串及其结尾的 null 字符。The destination string must be large enough to hold the source string and its terminating null character. 如果源和目标字符串重叠,则 strcpy_s 的行为是未定义的。The behavior of strcpy_s is undefined if the source and destination strings overlap.

wcscpy_s 是宽字符版本的 strcpy_s_mbscpy_s 是多字节字符版本。wcscpy_s is the wide-character version of strcpy_s, and _mbscpy_s is the multibyte-character version. wcscpy_s 的参数和返回值是宽字符字符串;而 _mbscpy_s 的则是多字节字符字符串。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.

如果 strDestinationstrSource 是空指针,或者如果目标字符串过小,则调用无效参数处理程序,如参数验证中所述。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. 如果允许继续执行,则在 strDestinationstrSource 是 null 指针时,这些函数将返回 EINVAL 并将 errno 设置为 EINVAL;如果目标字符串过小,则它们将返回 ERANGE 并将 errno 设置为 ERANGEIf execution is allowed to continue, these functions return EINVAL and set errno to EINVAL when strDestination or strSource is a null pointer, and they return ERANGE and set errno to ERANGE when the destination string is too small.

成功执行时,目标字符串始终以 null 结尾。Upon successful execution, the destination string is always null-terminated.

在 C++ 中,模板重载简化了这些函数的使用;重载可以自动推断缓冲区长度,从而无需指定大小自变量;并且它们可以自动将较旧、不安全的函数替换为更新、更安全的函数。In C++, use of these functions is simplified by template overloads that can infer buffer length automatically so that you don't have to specify a size argument, and they can automatically replace older, less-secure functions with their newer, more secure counterparts. 有关详细信息,请参阅 Secure Template OverloadsFor more information, see Secure Template Overloads.

这些函数的调试版本首先用 0xFE 填充缓冲区。The debug versions of these functions first fill the buffer with 0xFE. 若要禁用此行为,请使用 _CrtSetDebugFillThresholdTo disable this behavior, use _CrtSetDebugFillThreshold.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_tcscpy_s strcpy_s _mbscpy_s wcscpy_s


例程所返回的值Routine 必需的标头Required header
strcpy_s <string.h><string.h>
wcscpy_s <string.h> 或 <wchar.h><string.h> or <wchar.h>
_mbscpy_s <mbstring.h><mbstring.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.


// crt_strcpy_s.cpp  
// This program uses strcpy_s and strcat_s  
// to build a phrase.  

#include <string.h>  
#include <stdlib.h>  
#include <stdio.h>  
#include <errno.h>  

int main( void )  
   char string[80];  
   // using template versions of strcpy_s and strcat_s:  
   strcpy_s( string, "Hello world from " );  
   strcat_s( string, "strcpy_s " );  
   strcat_s( string, "and " );  
   // of course we can supply the size explicitly if we want to:  
   strcat_s( string, _countof(string), "strcat_s!" );  

   printf( "String = %s\n", string );  
String = Hello world from strcpy_s and strcat_s!  

请参阅See Also

字符串操作 String Manipulation
strcat、wcscat、_mbscat strcat, wcscat, _mbscat
strcmp、wcscmp、_mbscmp strcmp, wcscmp, _mbscmp
strncat_s、_strncat_s_l、wcsncat_s、_wcsncat_s_l、_mbsncat_s、_mbsncat_s_l strncat_s, _strncat_s_l, wcsncat_s, _wcsncat_s_l, _mbsncat_s, _mbsncat_s_l
strncmp、wcsncmp、_mbsncmp、_mbsncmp_l strncmp, wcsncmp, _mbsncmp, _mbsncmp_l
strncpy_s、_strncpy_s_l、wcsncpy_s、_wcsncpy_s_l、_mbsncpy_s、_mbsncpy_s_l strncpy_s, _strncpy_s_l, wcsncpy_s, _wcsncpy_s_l, _mbsncpy_s, _mbsncpy_s_l
_strnicmp、_wcsnicmp、_mbsnicmp、_strnicmp_l、_wcsnicmp_l、_mbsnicmp_l _strnicmp, _wcsnicmp, _mbsnicmp, _strnicmp_l, _wcsnicmp_l, _mbsnicmp_l
strrchr、wcsrchr、_mbsrchr、_mbsrchr_l strrchr, wcsrchr, _mbsrchr, _mbsrchr_l
strspn、wcsspn、_mbsspn、_mbsspn_lstrspn, wcsspn, _mbsspn, _mbsspn_l