Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      wcsrtombs, wcsrtombs_s

      From cppreference.com
      <c‎ |string‎ |multibyte
       
       
       
       
      Defined in header<wchar.h>
      (1)
      size_t wcsrtombs(char*dst,constwchar_t**src,size_t len,mbstate_t* ps);
      (since C95)
      (until C99)
      size_t wcsrtombs(char*restrict dst,constwchar_t**restrict src,size_t len,
                       mbstate_t*restrict ps);
      (since C99)
      errno_t wcsrtombs_s(size_t*restrict retval,char*restrict dst, rsize_t dstsz,

                           constwchar_t**restrict src, rsize_t len,

                           mbstate_t*restrict ps);
      (2)(since C11)
      1) Converts a sequence of wide characters from the array whose first element is pointed to by*src to its narrow multibyte representation that begins in the conversion state described by*ps. Ifdst is not null, converted characters are stored in the successive elements of the char array pointed to bydst. No more thanlen bytes are written to the destination array. Each character is converted as if by a call towcrtomb. The conversion stops if:
      • The null characterL'\0' was converted and stored. The bytes stored in this case are the unshift sequence (if necessary) followed by'\0',*src is set to null pointer value and*ps represents the initial shift state.
      • Awchar_t was found that does not correspond to a valid character in the current C locale.*src is set to point at the first unconverted wide character.
      • the next multibyte character to be stored would exceedlen.*src is set to point at the first unconverted wide character. This condition is not checked ifdst is a null pointer.
      2) Same as(1), except that
      • the function returns its result as an out-parameterretval
      • if the conversion stops without writing a null character, the function will store'\0' in the next byte indst, which may bedst[len] ordst[dstsz], whichever comes first (meaning up to len+1/dstsz+1 total bytes may be written). In this case, there may be no unshift sequence written before the terminating null.
      • the function clobbers the destination array from the terminating null and untildstsz
      • Ifsrc anddst overlap, the behavior is unspecified.
      • the following errors are detected at runtime and call the currently installedconstraint handler function:
      • retval,ps,src, or*src is a null pointer
      • dstsz orlen is greater thanRSIZE_MAX (unlessdst is null)
      • dstsz is not zero (unlessdst is null)
      • len is greater thandstsz and the conversion does not encounter null or encoding error in thesrc array by the timedstsz is reached (unlessdst is null)
      As with all bounds-checked functions,wcsrtombs_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

      dst - pointer to narrow character array where the multibyte characters will be stored
      src - pointer to pointer to the first element of a null-terminated wide string
      len - number of bytes available in the array pointed to by dst
      ps - pointer to the conversion state object
      dstsz - max number of bytes that will be written (size of thedst array)
      retval - pointer to asize_t object where the result will be stored

      [edit]Return value

      1) On success, returns the number of bytes (including any shift sequences, but excluding the terminating'\0') written to the character array whose first element is pointed to bydst. Ifdst is a null pointer, returns the number of bytes that would have been written. On conversion error (if invalid wide character was encountered), returns(size_t)-1, storesEILSEQ inerrno, and leaves*ps in unspecified state.
      2) Returns zero on success (in which case the number of bytes excluding terminating zero that were, or would be written todst, is stored in*retval), non-zero on error. In case of a runtime constraint violation, stores(size_t)-1 in*retval (unlessretval is null) and setsdst[0] to'\0' (unlessdst is null ordstmax is zero or greater thanRSIZE_MAX)

      [edit]Example

      Run this code
      #include <stdio.h>#include <locale.h>#include <string.h>#include <wchar.h> void print_wide(constwchar_t* wstr){mbstate_t state;memset(&state,0,sizeof state);size_t len=1+ wcsrtombs(NULL,&wstr,0,&state);char mbstr[len];    wcsrtombs(mbstr,&wstr, len,&state);printf("Multibyte string: %s\n", mbstr);printf("Length, including '\\0': %zu\n", len);} int main(void){setlocale(LC_ALL,"en_US.utf8");    print_wide(L"z\u00df\u6c34\U0001f34c");// or L"zß水🍌"}

      Output:

      Multibyte string: zß水🍌Length, including '\0': 11

      [edit]References

      • C17 standard (ISO/IEC 9899:2018):
      • 7.29.6.4.2 The wcsrtombs function (p: 324-325)
      • K.3.9.3.2.2 The wcsrtombs_s function (p: 471-472)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.29.6.4.2 The wcsrtombs function (p: 446)
      • K.3.9.3.2.2 The wcsrtombs_s function (p: 649-651)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.24.6.4.2 The wcsrtombs function (p: 392)

      [edit]See also

      converts a wide string to narrow multibyte character string
      (function)[edit]
      converts a wide character to its multibyte representation, given state
      (function)[edit]
      converts a narrow multibyte character string to wide string, given state
      (function)[edit]
      C++ documentation forwcsrtombs
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/string/multibyte/wcsrtombs&oldid=142642"

      [8]ページ先頭

      ©2009-2025 Movatter.jp