strnlen, strnlen_s, wcsnlen, wcsnlen_s, _mbsnlen, _mbsnlen_l, _mbstrnlen, _mbstrnlen_l

현재 로캘이나 전달된 로캘을 사용하여 문자열 길이를 가져옵니다. 이러한 함수는 보다 안전한 버전의 strlen, wcslen, _mbslen, _mbslen_l. _mbstrlen_l_mbstrlen

Important

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

구문

size_t strnlen(
   const char *str,
   size_t numberOfElements
);
size_t strnlen_s(
   const char *str,
   size_t numberOfElements
);
size_t wcsnlen(
   const wchar_t *str,
   size_t numberOfElements
);
size_t wcsnlen_s(
   const wchar_t *str,
   size_t numberOfElements
);
size_t _mbsnlen(
   const unsigned char *str,
   size_t numberOfElements
);
size_t _mbsnlen_l(
   const unsigned char *str,
   size_t numberOfElements,
   _locale_t locale
);
size_t _mbstrnlen(
   const char *str,
   size_t numberOfElements
);
size_t _mbstrnlen_l(
   const char *str,
   size_t numberOfElements,
   _locale_t locale
);

매개 변수

str
Null 종료 문자열입니다.

numberOfElements
문자열 버퍼의 크기입니다.

locale
사용할 로캘입니다.

반환 값

이러한 함수는 null 종결 문자를 제외하고 문자열에 있는 문자의 수를 반환합니다. 문자열의 첫 번째 numberOfElements 바이트(또는 와이드 문자)numberOfElements에 null 종결자가 없으면 오류 조건을 나타내기 위해 wcsnlen반환됩니다. null로 끝나는 문자열의 길이는 엄격히 낮numberOfElements습니다.

문자열에 잘못된 멀티바이트 문자가 있는 경우 _mbstrnlen_mbstrnlen_l은 -1을 반환합니다.

설명

참고 항목

strnlenstrlen을 대체하지 않습니다. strnlen은 네트워크 패킷과 같은 알려진 버퍼 크기로 들어오는 신뢰할 수 없는 데이터의 크기를 계산하는 데에만 사용할 수 있습니다. strnlen은 길이를 계산하지만 문자열이 종료되지 않은 경우 버퍼의 끝까지 진행하지 않습니다. 다른 상황에서는 strlen을 사용합니다. wcsnlen, _mbsnlen_mbstrnlen에도 동일하게 적용됩니다.

이러한 각 함수는 null 종결 문자를 제외하고 str에 있는 문자의 수를 반환합니다. 그러나 strnlenstrnlen_s는 싱글바이트 문자열로 문자열을 해석하므로 문자열에 멀티바이트 문자가 포함되어 있더라도 반환 값은 항상 바이트 수와 동일합니다. wcsnlenwcsnlen_s는 각각 strnlenstrnlen_s의 와이드 문자 버전입니다.wcsnlenwcsnlen_s의 인수는 와이드 문자열이고 문자 수는 와이드 문자 단위로 표시됩니다. 그렇지 않으면 wcsnlenstrnlenstrnlen_swcsnlen_s와 동일하게 작동합니다.

strnlen, wcsnlen_mbsnlen 해당 매개 변수의 유효성을 검사하지 않습니다. strNULL인 경우 액세스 위반이 발생합니다.

strnlen_swcsnlen_s는 자신의 매개 변수에 대한 유효성을 검사합니다. strNULL인 경우 함수는 0을 반환합니다.

또한 _mbstrnlen은 자신의 매개 변수에 대한 유효성을 검사합니다. 이 경우 str 또는 보다 _mbstrnlenINT_MAX큰 경우 numberOfElements 매개 변수 유효성 검사에 설명된 대로 잘못된 매개 변수 예외를 생성합니다.NULL 계속해서 실행하도록 허용한 경우 _mbstrnlenerrnoEINVAL로 설정하고 -1을 반환합니다.

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

일반 텍스트 루틴 매핑

TCHAR.H 루틴 _UNICODE 정의 _MBCS 되지 않음 _MBCS 정의 _UNICODE 정의
_tcsnlen strnlen strnlen wcsnlen
_tcscnlen strnlen _mbsnlen wcsnlen
_tcscnlen_l strnlen _mbsnlen_l wcsnlen

_mbsnlen_mbstrnlen은 멀티바이트 문자열의 멀티바이트 문자 수를 반환합니다. _mbsnlen 는 현재 사용 중인 멀티바이트 코드 페이지 또는 전달된 로캘에 따라 멀티바이트 문자 시퀀스를 인식합니다. 멀티바이트 문자 유효성을 테스트하지 않습니다. _mbstrnlen은 멀티바이트 문자의 유효성을 테스트하고 멀티바이트 문자 시퀀스를 인식합니다. _mbstrnlen에 전달된 문자열에 잘못된 멀티바이트 문자가 포함된 경우 errnoEILSEQ로 설정됩니다.

출력 값은 로캘의 LC_CTYPE 범주 설정 설정의 영향을 받습니다. 자세한 내용은 setlocale를 참조하세요. 이러한 함수 버전은 _l 접미사가 없는 함수는 로캘 종속 동작에 현재 로캘을 사용하고 _l 접미사가 있는 함수는 전달된 로캘 매개 변수를 대신 사용한다는 점을 제외하고는 동일합니다. 자세한 내용은 Locale을 참조하세요.

요구 사항

루틴에서 반환된 값 필수 헤더
strnlen, strnlen_s <string.h>
wcsnlen, wcsnlen_s <string.h> 또는 <wchar.h>
_mbsnlen, _mbsnlen_l <mbstring.h>
_mbstrnlen, _mbstrnlen_l <stdlib.h>

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

예시

// crt_strnlen.c

#include <string.h>

int main()
{
   // str1 is 82 characters long. str2 is 159 characters long

   char* str1 = "The length of a string is the number of characters\n"
               "excluding the terminating null.";
   char* str2 = "strnlen takes a maximum size. If the string is longer\n"
                "than the maximum size specified, the maximum size is\n"
                "returned rather than the actual size of the string.";
   size_t len;
   size_t maxsize = 100;

   len = strnlen(str1, maxsize);
   printf("%s\n Length: %d \n\n", str1, len);

   len = strnlen(str2, maxsize);
   printf("%s\n Length: %d \n", str2, len);
}
The length of a string is the number of characters
excluding the terminating null.
Length: 82

strnlen takes a maximum size. If the string is longer
than the maximum size specified, the maximum size is
returned rather than the actual size of the string.
Length: 100

참고 항목

문자열 조작
Locale
멀티바이트 문자 시퀀스 해석
setlocale, _wsetlocale
strncat, _strncat_l, wcsncat, _wcsncat_l, _mbsncat, _mbsncat_l
strncmp, wcsncmp, _mbsncmp, _mbsncmp_l
strcoll 함수
strncpy_s, _strncpy_s_l, wcsncpy_s, _wcsncpy_s_l, _mbsncpy_s, _mbsncpy_s_l
strrchr, wcsrchr, _mbsrchr, _mbsrchr_l
_strset, _strset_l, _wcsset, _wcsset_l, _mbsset, _mbsset_l
strspn, wcsspn, _mbsspn, _mbsspn_l