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.

语法Syntax

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

参数Parameters

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

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

返回值Return Value

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

备注Remarks

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.

如果读取的第一个字符,文件尾字符开头的存储 null 字符缓冲区NULL返回。If 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_s是宽字符版本的gets_s; 其自变量和返回值都是宽字符字符串。_getws_s is a wide-character version of gets_s; its argument and return value are wide-character strings.

如果缓冲区NULLsizeInCharacters小于或等于零,或如果缓冲区太小,无法包含的输入的行和 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

要求Requirements

例程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. 控制台中,与关联的标准流句柄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.

示例Example

// 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