gets_s、_getws_sgets_s, _getws_s

stdin流中获取一行。Gets a line from the stdin stream. 这些版本的 gets、_getws 具有安全增强功能,如 CRT 中的安全功能所述。These versions of gets, _getws have security enhancements, as described in Security Features in the CRT.


char *gets_s(
   char *buffer,
   size_t sizeInCharacters
wchar_t *_getws_s(
   wchar_t *buffer,
   size_t sizeInCharacters
template <size_t size>
char *gets_s( char (&buffer)[size] ); // C++ only

template <size_t size>
wchar_t *_getws_s( wchar_t (&buffer)[size] ); // C++ only


输入字符串的存储位置。Storage location for input string.

缓冲区的大小。The size of the buffer.

返回值Return Value

如果成功,则返回缓冲区Returns buffer if successful. NULL 指针指示错误或文件尾条件。A NULL pointer indicates an error or end-of-file condition. 使用 ferrorfeof 确定已发生哪种情况。Use ferror or feof to determine which one has occurred.


Gets_s函数从标准输入流stdin读取一行,并将其存储在缓冲区中。The gets_s function reads a line from the standard input stream stdin and stores it in buffer. 该行由第一个换行符(“\n”)之前的所有字符和该换行符构成。The line consists of all characters up to and including the first newline character ('\n'). 然后, gets_s在返回行之前,将换行符替换为 null 字符(' \ 0 ')。gets_s then replaces the newline character with a null character ('\0') before returning the line. 与此相反, fgets_s函数将保留换行符。In contrast, the fgets_s function retains the newline character.

如果读取的第一个字符是文件尾字符,则在缓冲区开头存储空字符,并返回nullIf the first character read is the end-of-file character, a null character is stored at the beginning of buffer and NULL is returned.

_getws_sgets_s的宽字符版本;其参数和返回值是宽字符字符串。_getws_s is a wide-character version of gets_s; its argument and return value are wide-character strings.

如果bufferNULLsizeInCharacters小于或等于零,或者如果缓冲区太小而无法包含输入行和 NULL 终止符,则这些函数将调用无效参数处理程序,如参数中所述。 验证If buffer is NULL or sizeInCharacters is less than or equal to zero, or if the buffer is too small to contain the input line and null terminator, these functions invoke an invalid parameter handler, as described in Parameter Validation. 如果允许执行继续,则这些函数将返回NULL ,并将 errno 设置为ERANGEIf execution is allowed to continue, these functions return NULL and set errno to ERANGE.

在 C++ 中,使用这些函数由模板重载简化;重载可以自动推导出缓冲区长度 (不再需要指定大小自变量),并且它们可以自动用以更新、更安全的对应物替换旧的、不安全的函数。In C++, using these functions is simplified by template overloads; the overloads can infer buffer length automatically (eliminating the need to specify a size argument) and they can automatically replace older, non-secure functions with their newer, secure counterparts. 有关详细信息,请参阅 Secure Template OverloadsFor more information, see Secure Template Overloads.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_getts_s_getts_s gets_sgets_s gets_sgets_s _getws_s_getws_s


例程所返回的值Routine 必需的标头Required header
gets_sgets_s <stdio.h><stdio.h>
_getws_s_getws_s <stdio.h> 或 <wchar.h><stdio.h> or <wchar.h>

通用 Windows 平台 (UWP) 应用中不支持控制台。The console is not supported in Universal Windows Platform (UWP) apps. 与控制台、 stdinstdoutstderr关联的标准流句柄必须重定向, 然后 C 运行时函数才能在 UWP 应用中使用它们。The standard stream handles that are associated with the console, stdin, stdout, and stderr, must be redirected before C run-time functions can use them in UWP apps. 有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.


// crt_gets_s.c
// This program retrieves a string from the stdin and
// prints the same string to the console.

#include <stdio.h>

int main( void )
   char line[21]; // room for 20 chars + '\0'
   gets_s( line, 20 );
   printf( "The line entered was: %s\n", line );
Hello there!
The line entered was: Hello there!

请参阅See also

流 I/OStream I/O
gets、_getwsgets, _getws
fgets、fgetwsfgets, fgetws
fputs、fputwsfputs, fputws
puts、_putwsputs, _putws