Share via


strcpy, wcscpy, _mbscpy

将一个字符串。 这些功能的更安全版本可用;请参见 strcpy_s, wcscpy_s, _mbscpy_s

重要

_mbscpy 不能在 Windows 运行时执行的应用程序。有关更多信息,请参见 CRT 函数不支持与 /ZW

char *strcpy(
   char *strDestination,
   const char *strSource 
);
wchar_t *wcscpy(
   wchar_t *strDestination,
   const wchar_t *strSource 
);
unsigned char *_mbscpy(
   unsigned char *strDestination,
   const unsigned char *strSource 
);
template <size_t size>
char *strcpy(
   char (&strDestination)[size],
   const char *strSource 
); // C++ only
template <size_t size>
wchar_t *wcscpy(
   wchar_t (&strDestination)[size],
   const wchar_t *strSource 
); // C++ only
template <size_t size>
unsigned char *_mbscpy(
   unsigned char (&strDestination)[size],
   const unsigned char *strSource 
); // C++ only

参数

  • strDestination
    目标字符串。

  • strSource
    null 终止的源字符串。

返回值

这些函数都返回一个目标字符串。 没有任何返回值保留指示错误。

备注

strcpy 功能复制 strSource,包括终止 null 字符,按 strDestination指定的位置。 如果源页和目标字符串重叠,strcpy 行为不确定。

安全说明安全说明

由于 strcpy 不检查在 strDestination 足够的空间,则复制 strSource之前,它是缓冲区溢出的一个可能的原因。因此,建议您使用 strcpy_s

wcscpy 和 _mbscpy 分别为,strcpy的,宽字符和多字节字符版本。 参数和返回 wcscpy 的值是宽字符字符串;这些 _mbscpy 的多字节字符字符串。 这三个功能否则具有相同的行为。

在 C++ 中,这些函数的调用的模板超负载越 + 新,保证这些函数副本。 有关更多信息,请参见安全模板重载

一般文本例程映射

TCHAR.H 实例

未定义的_UNICODE & _MBCS

定义的_MBCS

定义的_UNICODE

_tcscpy

strcpy

_mbscpy

wcscpy

要求

实例

必需的标头

strcpy

<string.h>

wcscpy

<string.h> 或 <wchar.h>

_mbscpy

<mbstring.h>

有关其他的兼容性信息,请参见 兼容性

示例

// crt_strcpy.c
// compile with: /W3
// This program uses strcpy
// and strcat to build a phrase.

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

int main( void )
{
   char string[80];

   // If you change the previous line to
   //   char string[20];
   // strcpy and strcat will happily overrun the string
   // buffer.  See the examples for strncpy and strncat
   // for safer string handling.

   strcpy( string, "Hello world from " ); // C4996
   // Note: strcpy is deprecated; use strcpy_s instead
   strcat( string, "strcpy " );           // C4996
   // Note: strcat is deprecated; use strcat_s instead
   strcat( string, "and " );              // C4996
   strcat( string, "strcat!" );           // C4996
   printf( "String = %s\n", string );
}
  

.NET Framework 等效项

System::String::Copy

请参见

参考

字符串操作(crt)

strcat, wcscat, _mbscat

strcmp, wcscmp, _mbscmp

strncat, _strncat_l, wcsncat, wcsncat_l, _mbsncat _mbsncat_l

strncmp, wcsncmp, _mbsncmp, _mbsncmp_l

strncpy, _strncpy_l, wcsncpy, _wcsncpy_l, _mbsncpy, _mbsncpy_l

_strnicmp, _wcsnicmp, _mbsnicmp, _strnicmp_l, _wcsnicmp_l, _mbsnicmp_l

strrchr, wcsrchr, _mbsrchr, _mbsrchr_l

strspn, wcsspn, _mbsspn, _mbsspn_l