将一个字符串字符到另一个。这些是 strncpy, _strncpy_l, wcsncpy, _wcsncpy_l, _mbsncpy, _mbsncpy_l 的版本与安全增强如 CRT中的安全功能所述。
 重要事项 | 
|---|
_mbsncpy_s 和 _mbsncpy_s_l 不能在运行时的窗口执行的应用程序。有关更多信息,请参见 CRT 函数不支持与 /ZW。  | 
errno_t strncpy_s(
   char *strDest,
   size_t numberOfElements,
   const char *strSource,
   size_t count
);
errno_t _strncpy_s_l(
   char *strDest,
   size_t numberOfElements,
   const char *strSource,
   size_t count,
   _locale_t locale
);
errno_t wcsncpy_s(
   wchar_t *strDest,
   size_t numberOfElements,
   const wchar_t *strSource,
   size_t count 
);
errno_t _wcsncpy_s_l(
   wchar_t *strDest,
   size_t numberOfElements,
   const wchar_t *strSource,
   size_t count,
   _locale_t locale
);
errno_t _mbsncpy_s(
   unsigned char *strDest,
   size_t numberOfElements,
   const unsigned char *strSource,
   size_t count 
);
errno_t _mbsncpy_s_l(
   unsigned char *strDest,
   size_t numberOfElements,
   const unsigned char *strSource,
   size_t count,
   locale_t locale
);
template <size_t size>
errno_t strncpy_s(
   char (&strDest)[size],
   const char *strSource,
   size_t count
); // C++ only
template <size_t size>
errno_t _strncpy_s_l(
   char (&strDest)[size],
   const char *strSource,
   size_t count,
   _locale_t locale
); // C++ only
template <size_t size>
errno_t wcsncpy_s(
   wchar_t (&strDest)[size],
   const wchar_t *strSource,
   size_t count 
); // C++ only
template <size_t size>
errno_t _wcsncpy_s_l(
   wchar_t (&strDest)[size],
   const wchar_t *strSource,
   size_t count,
   _locale_t locale
); // C++ only
template <size_t size>
errno_t _mbsncpy_s(
   unsigned char (&strDest)[size],
   const unsigned char *strSource,
   size_t count 
); // C++ only
template <size_t size>
errno_t _mbsncpy_s_l(
   unsigned char (&strDest)[size],
   const unsigned char *strSource,
   size_t count,
   locale_t locale
); // C++ only
参数
strDest
目标字符串。numberOfElements
目标字符串的大小,在字符。strSource
源字符串。count
要复制的字符数或 _TRUNCATE。locale
使用的区域设置。
返回值
零,如果成功,STRUNCATE,则截断发生的事件,否则错误代码。
错误状态
strDest  | 
numberOfElements  | 
strSource  | 
返回值  | 
strDest内容  | 
|---|---|---|---|---|
NULL  | 
any  | 
any  | 
EINVAL  | 
不修改  | 
any  | 
any  | 
NULL  | 
EINVAL  | 
strDest[0] 设置为 0  | 
any  | 
0  | 
any  | 
EINVAL  | 
不修改  | 
不是 NULL  | 
太小  | 
any  | 
ERANGE  | 
strDest[0] 设置为 0  | 
备注
这些功能复制 strSource 第一 D 字符。strDest,D 是较小者 count 和 strSource的长度。如果这些 D 字符在范围中容纳形式给出 numberOfElements) 的 strDest (和仍 null 结束符的空格,则这些字符复制,并终止 null 追加;否则,strDest[0] 设置为在 null 字符和无效参数调用处理程序,如 参数验证所述。
与异常上面段。如果 count 是 _TRUNCATE,则相同 strSource 与将放入 strDest 复制,同时仍然保留总是追加的终止 null 时空间。
例如,
char dst[5];
strncpy_s(dst, 5, "a long string", 5);
意味着我们请求 strncpy_s 长五个字符复制到缓冲区五个字节;这不会留下 null 结束符,因此 strncpy_s 零的空间该字符串和调用无效参数处理程序。
如果截断行为是必需的,使用 _TRUNCATE 或 (size – 1):
strncpy_s(dst, 5, "a long string", _TRUNCATE);
strncpy_s(dst, 5, "a long string", 4);
请注意不同 strncpy,因此,如果 count 比 strSource的长度时,目标字符串不用零填充该字符的长度 count。
如果源页和目标字符串重叠,strncpy_s 行为不确定。
如果 strDest 或 strSource 是 NULL,或者 numberOfElements 为 0,无效参数调用处理程序。如果执行允许继续,该函数返回 EINVAL 并将 errno 到 EINVAL。
wcsncpy_s 和 _mbsncpy_s 是 strncpy_s的宽字符和多字节字符版本。参数和返回 wcsncpy_s 的值,并 mbsncpy_s相应地改变。这六个功能否则具有相同的行为。
输出值受设置 LC_CTYPE 类设置的影响区域设置;请参见 setlocale 有关更多信息。这些功能的版本不 _l 后缀为该区域设置相关的行为使用当前区域设置;与 _l 后缀的版本相同,只不过它们使用传入的区域设置参数。有关更多信息,请参见区域设置。
在 C++ 中,使用这些功能由模板超加载简化;超加载可能推断缓冲区长度 (自动不再需要指定范围参数),并且还可以用以较新,安全重复自动替换旧,不安全的功能。有关更多信息,请参见安全模板重载。
这些函数的" debug "版本用 0xFD 首先加载缓冲区。若要禁用此行为,请使用 _CrtSetDebugFillThreshold。
一般文本例程映射
TCHAR.H 实例  | 
未定义的_UNICODE & _MBCS  | 
定义的_MBCS  | 
定义的_UNICODE  | 
|---|---|---|---|
_tcsncpy_s  | 
strncpy_s  | 
_mbsnbcpy_s  | 
wcsncpy_s  | 
_tcsncpy_s_l  | 
_strncpy_s_l  | 
_mbsnbcpy_s_l  | 
_wcsncpy_s_l  | 
 说明 | 
|---|
_strncpy_s_l、_wcsncpy_s_l 和 _mbsncpy_s_l 没有区域设置依赖项以及 _tcsncpy_s_l 提供并且不应直接调用。  | 
要求
实例  | 
必需的标头  | 
|---|---|
strncpy_s, _strncpy_s_l  | 
<string.h>  | 
wcsncpy_s, _wcsncpy_s_l  | 
<string.h> 或 <wchar.h>  | 
_mbsncpy_s, _mbsncpy_s_l  | 
<mbstring.h>  | 
有关其他的兼容性信息,请参见中介绍的 兼容性。
示例
// crt_strncpy_s_1.cpp
// compile with: /MTd
// these #defines enable secure template overloads
// (see last part of Examples() below)
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1 
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <crtdbg.h>  // For _CrtSetReportMode
#include <errno.h>
// This example uses a 10-byte destination buffer.
errno_t strncpy_s_tester( const char * src,
                          int count )
{
   char dest[10];
   printf( "\n" );
   if ( count == _TRUNCATE )
      printf( "Copying '%s' to %d-byte buffer dest with truncation semantics\n",
               src, _countof(dest) );
   else
      printf( "Copying %d chars of '%s' to %d-byte buffer dest\n",
              count, src, _countof(dest) );
   errno_t err = strncpy_s( dest, _countof(dest), src, count );
   printf( "    new contents of dest: '%s'\n", dest );
   return err;
}
void Examples()
{
   strncpy_s_tester( "howdy", 4 );
   strncpy_s_tester( "howdy", 5 );
   strncpy_s_tester( "howdy", 6 );
   printf( "\nDestination buffer too small:\n" );
   strncpy_s_tester( "Hi there!!", 10 );
   printf( "\nTruncation examples:\n" );
   errno_t err = strncpy_s_tester( "How do you do?", _TRUNCATE );
   printf( "    truncation %s occur\n", err == STRUNCATE ? "did"
                                                       : "did not" );
   err = strncpy_s_tester( "Howdy.", _TRUNCATE );
   printf( "    truncation %s occur\n", err == STRUNCATE ? "did"
                                                       : "did not" );
   printf( "\nSecure template overload example:\n" );
   char dest[10];
   strncpy( dest, "very very very long", 15 );
   // With secure template overloads enabled (see #defines at
   // top of file), the preceding line is replaced by
   //    strncpy_s( dest, _countof(dest), "very very very long", 15 );
   // Instead of causing a buffer overrun, strncpy_s invokes
   // the invalid parameter handler.
   // If secure template overloads were disabled, strncpy would
   // copy 15 characters and overrun the dest buffer.
   printf( "    new contents of dest: '%s'\n", dest );
}
void myInvalidParameterHandler(
   const wchar_t* expression,
   const wchar_t* function, 
   const wchar_t* file, 
   unsigned int line, 
   uintptr_t pReserved)
{
   wprintf(L"Invalid parameter handler invoked: %s\n", expression);
}
int main( void )
{
   _invalid_parameter_handler oldHandler, newHandler;
   newHandler = myInvalidParameterHandler;
   oldHandler = _set_invalid_parameter_handler(newHandler);
   // Disable the message box for assertions.
   _CrtSetReportMode(_CRT_ASSERT, 0);
   Examples();
}
// crt_strncpy_s_2.c
// contrasts strncpy and strncpy_s
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
   char a[20] = "test";
   char s[20];
   // simple strncpy usage:
   strcpy_s( s, 20, "dogs like cats" );
   printf( "Original string:\n   '%s'\n", s );
   // Here we can't use strncpy_s since we don't 
   // want null termination
   strncpy( s, "mice", 4 );
   printf( "After strncpy (no null-termination):\n   '%s'\n", s );
   strncpy( s+5, "love", 4 );
   printf( "After strncpy into middle of string:\n   '%s'\n", s );
   // If we use strncpy_s, the string is terminated 
   strncpy_s( s, _countof(s), "mice", 4 );
   printf( "After strncpy_s (with null-termination):\n   '%s'\n", s );
}
.NET Framework 等效项
请参见
参考
strncat_s, _strncat_s_l, wcsncat_s, _wcsncat_s_l, _mbsncat_s, _mbsncat_s_l
strncmp, wcsncmp, _mbsncmp, _mbsncmp_l
_strnicmp, _wcsnicmp, _mbsnicmp, _strnicmp_l, _wcsnicmp_l, _mbsnicmp_l
strrchr, wcsrchr, _mbsrchr, _mbsrchr_l
重要事项
说明