strcpy、wcscpy、_mbscpy

复制字符串。 有关这些函数的更多安全版本,请参见 strcpy_s、wcscpy_s、_mbscpy_s

重要

_mbscpy 不能用于在 Windows 运行时 中执行的应用程序。有关详细信息,请参见 CRT functions not supported with /ZW(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 在复制 strSource 前不会检查是否 strDestination 有的足够空间,因此这是一个可能导致缓冲区溢出的潜在原因。因此,我们建议你用 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