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  


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

[in] sizeInCharacters[in] sizeInCharacters
缓冲区的大小。The size of the buffer.

返回值Return Value

如果成功,则返回 bufferReturns 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 中读取一个行并将该行存储在 buffer 中。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.

如果读取的第一个字符是文件尾字符,则空字符将存储在 buffer 的开头,并返回 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.

_getwsgets_s 的宽字符版本;其参数和返回值都是宽字符字符串。_getws 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 gets_s gets_s _getws


例程Routine 必需的标头Required header
gets_s <stdio.h><stdio.h>
_getws <stdio.h> 或 <wchar.h><stdio.h> or <wchar.h>

通用 Windows 平台 (UWP) 应用中不支持控制台。The console is not supported in Universal Windows Platform (UWP) apps. 控制台中,与关联的标准流句柄stdinstdout,和stderr,必须将重定向,然后 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/O Stream I/O
gets、_getws gets, _getws
fgets、fgetws fgets, fgetws
fputs、fputws fputs, fputws
puts、_putwsputs, _putws