Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      strerror, strerror_s, strerrorlen_s

      From cppreference.com
      <c‎ |string‎ |byte
       
       
       
       
      Defined in header<string.h>
      char* strerror(int errnum);
      (1)
      errno_t strerror_s(char*buf, rsize_t bufsz, errno_t errnum);
      (2)(since C11)
      size_t strerrorlen_s( errno_t errnum);
      (3)(since C11)
      1) Returns a pointer to the textual description of the system error codeerrnum, identical to the description that would be printed byperror().
      errnum is usually acquired from theerrno variable, however the function accepts any value of typeint. The contents of the string are locale-specific.
      The returned string must not be modified by the program, but may be overwritten by a subsequent call to thestrerror function.strerror is not required to be thread-safe. Implementations may be returning different pointers to static read-only string literals or may be returning the same pointer over and over, pointing at a static buffer in which strerror places the string.
      2) Same as(1), except that the message is copied into user-provided storagebuf. No more thanbufsz-1 bytes are written, the buffer is always null-terminated. If the message had to be truncated to fit the buffer andbufsz is greater than 3, then onlybufsz-4 bytes are written, and the characters"..." are appended before the null terminator. In addition, the following errors are detected at runtime and call the currently installedconstraint handler function:
      • buf is a null pointer
      • bufsz is zero or greater thanRSIZE_MAX
      The behavior is undefined if writing tobuf occurs past the end of the array, which can happen when the size of the buffer pointed to bybuf is less than the number of characters in the error message which in turn is less thanbufsz.
      3) Computes the length of the untruncated locale-specific error message thatstrerror_s would write if it were called witherrnum. The length does not include the null terminator.
      As with all bounds-checked functions,strerror_s andstrerrorlen_s are 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<string.h>.

      Contents

      [edit]Parameters

      errnum - integral value referring to an error code
      buf - pointer to a user-provided buffer
      bufsz - size of the user-provided buffer

      [edit]Return value

      1) Pointer to a null-terminated byte string corresponding to theerrno error codeerrnum.
      2) Zero if the entire message was successfully stored inbuf, non-zero otherwise.
      3) Length (not including the null terminator) of the message thatstrerror_s would return

      [edit]Notes

      POSIX allows subsequent calls tostrerror to invalidate the pointer value returned by an earlier call. It also specifies that it is theLC_MESSAGES locale facet that controls the contents of these messages.

      strerror_s is the only bounds-checked function that allows truncation, because providing as much information as possible about a failure was deemed to be more desirable. POSIX also definesstrerror_r for similar purposes.

      [edit]Example

      Run this code
      #define __STDC_WANT_LIB_EXT1__ 1#include <stdio.h>#include <errno.h>#include <string.h>#include <locale.h> int main(void){FILE*fp=fopen(tmpnam((char[L_tmpnam]){0}),"r");if(fp==NULL){printf("File opening error: %s\n", strerror(errno));setlocale(LC_MESSAGES,"de_DE.utf8");printf("Now in German: %s\n", strerror(errno));#ifdef __STDC_LIB_EXT1__setlocale(LC_ALL,"ja_JP.utf8");// printf needs CTYPE for multibyte outputsize_t errmsglen= strerrorlen_s(errno)+1;char errmsg[errmsglen];         strerror_s(errmsg, errmsglen,errno);printf("Now in Japanese: %s\n", errmsg);#endif}}

      Possible output:

      File opening error: No such file or directoryNow in German: Datei oder Verzeichnis nicht gefundenNow in Japanese: そのようなファイル、又はディレクトリはありません

      [edit]References

      • C11 standard (ISO/IEC 9899:2011):
      • 7.24.6.2 The strerror function (p: 371)
      • K.3.7.4.2 The strerror_s function (p: 622)
      • K.3.7.4.3 The strerrorlen_s function (p: 623)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.21.6.2 The strerror function (p: 334)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 4.11.6.2 The strerror function

      [edit]See also

      displays a character string corresponding of the current error tostderr
      (function)[edit]
      macro which expands to POSIX-compatible thread-local error number variable
      (macro variable)[edit]
      C++ documentation forstrerror
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/string/byte/strerror&oldid=132557"

      [8]ページ先頭

      ©2009-2025 Movatter.jp