Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      gets, gets_s

      From cppreference.com
      <c‎ |io
       
       
      File input/output
      Types and objects
      Functions
      File access
      Unformatted input/output
      (C95)(C95)
      (C95)
      (C95)(C95)
      (C95)
      (C95)

      Formatted input
       
      Defined in header<stdio.h>
      char* gets(char* str);
      (1)(removed in C11)
      char* gets_s(char* str, rsize_t n);
      (2)(since C11)
      1) Readsstdin into the character array pointed to bystr until a newline character is found or end-of-file occurs. A null character is written immediately after the last character read into the array. The newline character is discarded but not stored in the buffer.
      2) Reads characters fromstdin until a newline is found or end-of-file occurs. Writes only at mostn-1 characters into the array pointed to bystr, and always writes the terminating null character (unlessstr is a null pointer). The newline character, if found, is discarded and does not count toward the number of characters written to the buffer.
      The following errors are detected at runtime and call the currently installedconstraint handler function:
      • n is zero;
      • n is greater thanRSIZE_MAX;
      • str is a null pointer;
      • endline oreof not encountered after storingn-1 characters to the buffer.
      In any case,gets_s first finishes reading and discarding the characters fromstdin until new-line character, end-of-file condition, or read error before calling the constraint handler.
      As with all bounds-checked functions,gets_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<stdio.h>.

      Contents

      [edit]Parameters

      str - a character array the characters fromstdin to be written to
      n - maximum number of characters that can be written to the array pointed to bystr

      [edit]Return value

      str on success, a null pointer on failure.

      If the failure has been caused by end of file condition, additionally sets theeof indicator (seefeof()) onstdin. If the failure has been caused by some other error, sets theerror indicator (seeferror()) onstdin.

      [edit]Notes

      Thegets() function does not perform bounds checking, therefore this function is extremely vulnerable to buffer-overflow attacks. It cannot be used safely (unless the program runs in an environment which restricts what can appear onstdin). For this reason, the function has been deprecated in the third corrigendum to the C99 standard and removed altogether in the C11 standard.fgets() andgets_s() are the recommended replacements.

      WARNING: Never usegets().

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • K.3.5.4.1 The gets_s function (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • K.3.5.4.1 The gets_s function (p: TBD)
      • C11 standard (ISO/IEC 9899:2011):
      • K.3.5.4.1 The gets_s function (p: 602-603)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.19.7.7 The gets function (p: 298)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 4.9.7.7 The gets function

      [edit]See also

      reads formatted input fromstdin, a file stream or a buffer
      (function)[edit]
      gets a character string from a file stream
      (function)[edit]
      writes a character string to a file stream
      (function)[edit]
      read from a stream into an automatically resized buffer until delimiter/end of line
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/io/gets&oldid=172391"

      [8]ページ先頭

      ©2009-2025 Movatter.jp