Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      wcsncat, wcsncat_s

      From cppreference.com
      <c‎ |string‎ |wide
       
       
       
      Null-terminated wide strings
       
      Defined in header<wchar.h>
      (1)
      wchar_t*wcsncat(wchar_t*dest,constwchar_t*src,size_t count);
      (since C95)
      (until C99)
      wchar_t*wcsncat(wchar_t*restrict dest,
                       constwchar_t*restrict src,size_t count);
      (since C99)
      errno_t wcsncat_s(wchar_t*restrict dest, rsize_t destsz,
                         constwchar_t*restrict src, rsize_t count);
      (2)(since C11)
      1) Appends at mostcount wide characters from the wide string pointed to bysrc, stopping if the null terminator is copied, to the end of the character string pointed to bydest. The wide charactersrc[0] replaces the null terminator at the end ofdest. The null terminator is always appended in the end (so the maximum number of wide characters the function may write iscount+1).
      The behavior is undefined if the destination array is not large enough for the contents of bothstr anddest and the terminating null wide character.
      The behavior is undefined if the strings overlap.
      2) Same as(1), except that this function may clobber the remainder of the destination array (from the last wide character written todestsz) and that the following errors are detected at runtime and call the currently installedconstraint handler function:
      • src ordest is a null pointer
      • destsz orcount is zero or greater thanRSIZE_MAX/sizeof(wchar_t)
      • there is no null wide character in the firstdestsz wide characters ofdest
      • truncation would occur:count or the length ofsrc, whichever is less, exceeds the space available between the null terminator ofdest anddestsz.
      • overlap would occur between the source and the destination strings
      As with all bounds-checked functions,wcsncat_s is only guaranteed to be available if__STDC_LIB_EXT1__ is defined by the implementation and if the user defines__STDC_WANT_LIB_EXT1__ to the integer constant1 before including<wchar.h>.

      Contents

      [edit]Parameters

      dest - pointer to the null-terminated wide string to append to
      src - pointer to the null-terminated wide string to copy from
      count - maximum number of wide characters to copy
      destsz - the size of the destination buffer

      [edit]Return value

      1) returns a copy ofdest
      2) returns zero on success, returns non-zero on error. Also, on error, writesL'\0' todest[0] (unlessdest is a null pointer ordestsz is zero or greater thanRSIZE_MAX/sizeof(wchar_t)).

      [edit]Notes

      Although truncation to fit the destination buffer is a security risk and therefore a runtime constraints violation forwcsncat_s, it is possible to get the truncating behavior by specifyingcount equal to the size of the destination array minus one: it will copy the firstcount wide characters and append the null terminator as always:wcsncat_s(dst,sizeof dst/sizeof*dst, src,(sizeof dst/sizeof*dst)-wcsnlen_s(dst,sizeof dst/sizeof*dst)-1);

      [edit]Example

      Run this code
      #include <wchar.h>#include <stdio.h>#include <locale.h> int main(void){wchar_t str[50]= L"Земля, прощай.";    wcsncat(str, L" ",1);    wcsncat(str, L"В добрый путь.",8);// only append the first 8 wide charssetlocale(LC_ALL,"en_US.utf8");printf("%ls", str);}

      Possible output:

      Земля, прощай. В добрый

      [edit]References

      • C17 standard (ISO/IEC 9899:2018):
      • 7.29.4.3.2 The wcsncat function (p: 315)
      • K.3.9.2.2.2 The wcsncat_s function (p: 466-467)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.29.4.3.2 The wcsncat function (p: 432-433)
      • K.3.9.2.2.2 The wcsncat_s function (p: 643-644)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.24.4.3.2 The wcsncat function (p: 378-379)

      [edit]See also

      (C95)(C11)
      appends a copy of one wide string to another
      (function)[edit]
      concatenates a certain amount of characters of two strings
      (function)[edit]
      (C95)(C11)
      copies one wide string to another
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/string/wide/wcsncat&oldid=140336"

      [8]ページ先頭

      ©2009-2025 Movatter.jp