본문 바로가기
VisualStudio.C++.C#/코딩팁,함수활용,단편

문자열 복사. _tcscpy_s, strcpy_s, wcscpy_s, _mbscpy_s

by i.got.it 2016. 1. 18.



_tcscpy_s, strcpy_s, wcscpy_s, _mbscpy_s


Copies a string. These versions of strcpy, wcscpy, _mbscpy have security enhancements, as described in CRT의 보안 기능.

_mbscpy_s cannot be used in applications that execute in the Windows Runtime. For more information, see CRT functions not supported with /ZW.


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

strDestination

Location of the destination string buffer.

numberOfElements

Size of the destination string buffer in char units for narrow and multi-byte functions, and wchar_t units for wide functions.

strSource

Null-terminated source string buffer.

Zero if successful; otherwise, an 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

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. The destination string must be large enough to hold the source string and its terminating null character. The behavior of strcpy_s is undefined if the source and destination strings overlap.

wcscpy_s is the wide-character version of strcpy_s, and _mbscpy_s is the multibyte-character version. 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.

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 매개 변수 유효성 검사. If 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.

Upon successful execution, the destination string is always null-terminated.

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. For more information, see 안전한 템플릿 오버로드.

The debug versions of these functions first fill the buffer with 0xFE. 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

요구 사항

Routine

Required header

strcpy_s

<string.h>

wcscpy_s

<string.h> or <wchar.h>

_mbscpy_s

<mbstring.h>

For additional compatibility information, see 호환성.

예제

// 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!

from MSDN.

 본 글이 포함된 상위 정리 장소.

 

 Visual Studio/VC++/C/C# 활용정리 -> http://igotit.tistory.com/11

 

 


///561

댓글



 

비트코인




암호화폐       외환/나스닥/골드         암호화폐/외환/나스닥/골드
     
현물 |선물 인버스 |선물 USDT       전략매니저(카피트레이딩)         프랍 트레이더 온라인 지원가능. MT4,MT5