http://msdn.microsoft.com/zh-cn/library/td1esda9.aspx
errno_t strcpy_s(
char *strDestination,
size_t numberOfElements,
const char *strSource
);
template <size_t size>
errno_t strcpy_s(
char (&strDestination)[size],
const char *strSource
); // C++ only
Parameters 参数
strDestination
Location of destination string buffer
Return Value 返回值
Zero if successful; an error otherwise.成功返回0;否则返回一个error
strDestination | numberOfElements | strSource | Return value | Contents of strDestination |
---|---|---|---|---|
NULL | any | any | EINVAL | not modified |
any | any | NULL | EINVAL | strDestination[0] set to 0 |
any | 0, or too small | any | ERANGE | strDestination[0] set to 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.
此函数拷贝strSource指向地址的内容,包括空结束字符('/0'),到strDestination指定的位置。这个目标字符串必须足够大来容纳源串,包括结束空的字符。如果源串和目标串重叠,那么这个拷贝函数的结果是不确定的。
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.
TCHAR.H routine | _UNICODE & _MBCS not defined | _MBCS defined | _UNICODE defined |
---|---|---|---|
_tcscpy_s | strcpy_s | _mbscpy_s | wcscpy_s |
strcpy_s | <string.h> |
Example
// 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!