strncpy, _strncpy_l, wcsncpy, _wcsncpy_l, _mbsncpy, _mbsncpy_l

한 문자열의 문자를 다른 문자열로 복사합니다. 이러한 함수의 더 안전한 버전을 사용할 수 있습니다. 참조 strncpy_s, _strncpy_s_l, wcsncpy_s, _wcsncpy_s_l_mbsncpy_s, _mbsncpy_s_l.

Important

Windows 런타임에서 실행되는 애플리케이션에서는 _mbsncpy_mbsncpy_l을 사용할 수는 없습니다. 자세한 내용은 유니버설 Windows 플랫폼 앱에서 지원되지 않는 CRT 함수를 참조하세요.

구문

char *strncpy(
   char *strDest,
   const char *strSource,
   size_t count
);
char *_strncpy_l(
   char *strDest,
   const char *strSource,
   size_t count,
   _locale_t locale
);
wchar_t *wcsncpy(
   wchar_t *strDest,
   const wchar_t *strSource,
   size_t count
);
wchar_t *_wcsncpy_l(
   wchar_t *strDest,
   const wchar_t *strSource,
   size_t count,
   _locale_t locale
);
unsigned char *_mbsncpy(
   unsigned char *strDest,
   const unsigned char *strSource,
   size_t count
);
unsigned char *_mbsncpy_l(
   unsigned char *strDest,
   const unsigned char *strSource,
   size_t count,
   _locale_t locale
);
template <size_t size>
char *strncpy(
   char (&strDest)[size],
   const char *strSource,
   size_t count
); // C++ only
template <size_t size>
char *_strncpy_l(
   char (&strDest)[size],
   const char *strSource,
   size_t count,
   _locale_t locale
); // C++ only
template <size_t size>
wchar_t *wcsncpy(
   wchar_t (&strDest)[size],
   const wchar_t *strSource,
   size_t count
); // C++ only
template <size_t size>
wchar_t *_wcsncpy_l(
   wchar_t (&strDest)[size],
   const wchar_t *strSource,
   size_t count,
   _locale_t locale
); // C++ only
template <size_t size>
unsigned char *_mbsncpy(
   unsigned char (&strDest)[size],
   const unsigned char *strSource,
   size_t count
); // C++ only
template <size_t size>
unsigned char *_mbsncpy_l(
   unsigned char (&strDest)[size],
   const unsigned char *strSource,
   size_t count,
   _locale_t locale
); // C++ only

매개 변수

strDest
대상 문자열입니다.

strSource
소스 문자열입니다.

count
복사할 문자의 수입니다.

locale
사용할 로캘입니다.

반환 값

strDest을(를) 반환합니다. 반환 값 없음은 오류를 나타내는 데 예약되어 있습니다.

설명

strncpy 함수는 count의 초기 strSource 문자를 strDest로 복사하고 strDest를 반환합니다. 길이strSource보다 작거나 같은 경우 count null 문자가 복사된 문자열에 자동으로 추가되지 않습니다. countstrSource의 길이보다 크면 대상 문자열이 count 길이까지 null 문자로 채워집니다. 소스 문자열과 대상 문자열이 겹치는 경우 strncpy의 동작이 정의되지 않습니다.

Important

strncpystrDest에 공간이 충분한지 확인하지 않습니다. 이는 버퍼 오버런의 잠재적 원인이 될 수 있습니다. count 인수는 strDest의 크기가 아니라 복사되는 문자 수를 제한합니다. 다음 예를 참조하세요. 자세한 내용은 버퍼 오버런 방지를 참조 하세요.

strDest 포인터이거나 strSourceNULL 0보다 작거나 같은 경우 count 매개 변수 유효성 검사에 설명된 대로 잘못된 매개 변수 처리기가 호출됩니다. 계속해서 실행하도록 허용된 경우 이러한 함수가 -1을 반환하고 errnoEINVAL로 설정합니다.

wcsncpy_mbsncpystrncpy의 와이드 문자 및 멀티바이트 문자 버전입니다. 따라서 wcsncpy_mbsncpy의 반환 값과 인수도 달라집니다. 그 외의 경우에는 이들 6개 함수가 동일하게 작동합니다.

_l 접미사가 있는 이러한 함수 버전은 로캘 종속 동작에 현재 로캘 대신 전달된 로캘 매개 변수를 사용하는 경우를 제외하고는 동일합니다. 자세한 내용은 Locale을 참조하세요.

C++에서 이러한 함수는 보다 최신의 보안 대응 함수를 호출하는 템플릿 오버로드를 갖고 있습니다. 자세한 내용은 보안 템플릿 오버로드를 참조 하세요.

기본적으로 이 함수의 전역 상태는 애플리케이션으로 범위가 지정됩니다. 이 동작을 변경하려면 CRT의 전역 상태를 참조하세요.

일반 텍스트 루틴 매핑

TCHAR.H 루틴 _UNICODE 정의 _MBCS 되지 않음 _MBCS 정의 _UNICODE 정의
_tcsncpy strncpy _mbsnbcpy wcsncpy
_tcsncpy_l _strncpy_l _mbsnbcpy_l _wcsncpy_l

참고 항목

_strncpy_l_wcsncpy_l은 로캘에 종속되지 않습니다. 이러한 함수는 _tcsncpy_l용으로만 제공되며 직접 호출하면 안 됩니다.

요구 사항

루틴에서 반환된 값 필수 헤더
strncpy <string.h>
wcsncpy <string.h> 또는 <wchar.h>
_mbsncpy, _mbsncpy_l <mbstring.h>

플랫폼 호환성에 대한 자세한 내용은 호환성을 참조 하세요.

예시

다음 예제에서는 strncpy의 사용과 프로그램 버그 및 보안 문제를 야기하는 오용 사례를 보여 줍니다. 컴파일러는 다음과 유사한 crt_strncpy_x86.c(15) 각 호출에 strncpy 대한 경고를 생성합니다. 경고 C4996: 'strncpy': 이 함수 또는 변수는 안전하지 않을 수 있습니다. 대신 사용하는 strncpy_s 것이 좋습니다. 사용 중단을 사용하지 않으려면 .를 사용합니다_CRT_SECURE_NO_WARNINGS. 자세한 내용은 온라인 도움말을 참조하세요.

// crt_strncpy_x86.c
// Use this command in an x86 developer command prompt to compile:
// cl /TC /W3 crt_strncpy_x86.c

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

int main() {
   char t[20];
   char s[20];
   char *p = 0, *q = 0;

   strcpy_s(s, sizeof(s), "AA BB CC");
   // Note: strncpy is deprecated; consider using strncpy_s instead
   strncpy(s, "aa", 2);     // "aa BB CC"         C4996
   strncpy(s + 3, "bb", 2); // "aa bb CC"         C4996
   strncpy(s, "ZZ", 3);     // "ZZ",              C4996
                            // count greater than strSource, null added
   printf("%s\n", s);

   strcpy_s(s, sizeof(s), "AA BB CC");
   p = strstr(s, "BB");
   q = strstr(s, "CC");
   strncpy(s, "aa", p - s - 1);   // "aa BB CC"   C4996
   strncpy(p, "bb", q - p - 1);   // "aa bb CC"   C4996
   strncpy(q, "cc",  q - s);      // "aa bb cc"   C4996
   strncpy(q, "dd", strlen(q));   // "aa bb dd"   C4996
   printf("%s\n", s);

   // some problems with strncpy
   strcpy_s(s, sizeof(s), "test");
   strncpy(t, "this is a very long string", 20 ); // C4996
   // Danger: at this point, t has no terminating null,
   // so the printf continues until it runs into one.
   // In this case, it will print "this is a very long test"
   printf("%s\n", t);

   strcpy_s(t, sizeof(t), "dogs like cats");
   printf("%s\n", t);

   strncpy(t + 10, "to chase cars.", 14); // C4996
   printf("%s\n", t);

   // strncpy has caused a buffer overrun and corrupted string s
   printf("Buffer overrun: s = '%s' (should be 'test')\n", s);
   // Since the stack grows from higher to lower addresses, buffer
   // overruns can corrupt function return addresses on the stack,
   // which can be exploited to run arbitrary code.
}

출력

ZZ
aa bb dd
this is a very long test
dogs like cats
dogs like to chase cars.
Buffer overrun: s = 'ars.' (should be 'test')

자동 변수의 레이아웃과 오류 검색 및 코드 보호의 수준은 변경된 컴파일러 설정에 따라 달라질 수 있습니다. 이 예제에서는 다른 컴파일러 옵션을 사용하거나 다른 컴파일 환경에서 빌드하는 경우 서로 다른 결과가 발생할 수 있습니다.

참고 항목

문자열 조작
Locale
멀티바이트 문자 시퀀스 해석
_mbsnbcpy, _mbsnbcpy_l
strcat, wcscat, _mbscat
strcmp, wcscmp, _mbscmp
strcpy, wcscpy, _mbscpy
strncat, _strncat_l, wcsncat, _wcsncat_l, _mbsncat, _mbsncat_l
strncmp, wcsncmp, _mbsncmp, _mbsncmp_l
_strnicmp, _wcsnicmp, _mbsnicmp, _strnicmp_l, _wcsnicmp_l, _mbsnicmp_l
strrchr, wcsrchr, _mbsrchr, _mbsrchr_l
_strset, _strset_l, _wcsset, _wcsset_l, _mbsset, _mbsset_l
strspn, wcsspn, _mbsspn, _mbsspn_l
strncpy_s, _strncpy_s_l, wcsncpy_s, _wcsncpy_s_l, _mbsncpy_s, _mbsncpy_s_l
strcpy_s, wcscpy_s, _mbscpy_s