gets、_getwsgets, _getws

stdin 流中获取行。Gets a line from the stdin stream. 提供这些函数的更多安全版本;请参阅 gets_s, _getws_sMore secure versions of these functions are available; see gets_s, _getws_s.


这些函数已过时。These functions are obsolete. 从 Visual Studio 2015 开始,CRT 中不再提供这些函数。Beginning in Visual Studio 2015, they are not available in the CRT. 这些函数(gets_s 和 _getws_s)的安全版本仍然可用。The secure versions of these functions, gets_s and _getws_s, are still available. 有关这些备用函数的信息,请参阅 gets_s, _getws_sFor information on these alternative functions, see gets_s, _getws_s.


此 API 不能用于在 Windows 运行时中执行的应用程序。This API cannot be used in applications that execute in the Windows Runtime. 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数For more information, see CRT functions not supported in Universal Windows Platform apps.


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


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

返回值Return Value

如果成功,则返回其参数。Returns its argument 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. 如果 buffer 为 NULL,这些函数则会调用无效的参数处理程序,如参数验证中所述。If buffer is NULL, these functions invoke an invalid parameter handler, as described in Parameter Validation. 如果允许执行继续,则这些函数返回 NULL 并将 errno 设置为 EINVALIf execution is allowed to continue, these functions return NULL and set errno to EINVAL.


gets 函数从标准输入流 stdin 中读取一个行并将该行存储在 buffer 中。The gets 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 会将换行符替换为 null 字符(“\0”)。gets then replaces the newline character with a null character ('\0') before returning the line. 相反, fgets 函数将保留换行符。In contrast, the fgets function retains the newline character. _getwsgets的宽字符版本;其参数和返回值都是宽字符字符串。_getws is a wide-character version of gets; its argument and return value are wide-character strings.


由于无法限制 gets 读取的字符数,因此不受信任的输入容易导致缓冲区溢出。Because there is no way to limit the number of characters read by gets, untrusted input can easily cause buffer overruns. 请改用 fgetsUse fgets instead.

在 C++ 中,这些函数具有模板重载,以调用这些函数的更新、更安全副本。In C++, these functions have template overloads that invoke the newer, secure counterparts of these functions. 有关详细信息,请参阅 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 gets _getws


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

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.


// crt_gets.c  
// compile with: /WX /W3  

#include <stdio.h>  

int main( void )  
   char line[21]; // room for 20 chars + '\0'  
   gets( line );  // C4996  
   // Danger: No way to limit input to 20 chars.  
   // Consider using gets_s instead.  
   printf( "The line entered was: %s\n", line );  

请注意,20 个字符以上的输入将溢出行缓冲区,并且会导致程序发生崩溃。Note that input longer than 20 characters will overrun the line buffer and almost certainly cause the program to crash.

Hello there!The line entered was: Hello there!  

请参阅See Also

流 I/O Stream I/O
fgets、fgetws fgets, fgetws
fputs、fputws fputs, fputws
puts、_putwsputs, _putws