strcpy_s

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

numberOfElements

Size of the destination string buffer. 目标字符串缓冲区大小

strSource

Null-terminated source string buffer.

Return Value 返回值

Zero if successful; an error otherwise.成功返回0;否则返回一个error

 

Error Conditions 错误类型

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

 
Remarks 说明

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.

 

Generic-Text Routine Mappings

TCHAR.H routine

_UNICODE & _MBCS not defined

_MBCS defined

_UNICODE defined

_tcscpy_s

strcpy_s

_mbscpy_s

wcscpy_s

Requirements 头文件

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!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值