gets_s, _getws_s

获取 stdin 流的一行。 这些是 获取,_getws 的版本与安全增强如 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
    输入字符串的存储位置。

  • [in] sizeInCharacters
    缓冲区的大小。

返回值

如果成功,则返回 buffer。 NULL 指针表示错误或文件结尾条件。 使用 ferrorfeof 确定哪个发生。

备注

gets_s 函数在 buffer读取标准输入流 stdin 的一行并将其存储。 行包含所有字符模式将匹配第一个换行符 (“\n”)。 gets_s 使用 null 字符 (“\0 "然后替换换行符) 在返回行之前。 相反,fgets_s 功能保留换行符。

如果第一个字符是读取文件结尾字符,null 字符在 buffer 演练中,它 NULL 返回。

_getws 是 gets_s的宽字符版本;其参数和返回值是宽字符字符串。

如果 buffer 是 NULL 或 sizeInCharacters 小于或等于零,或者,如果缓冲区因过小而无法包含一行输入和 null 结束符,这些函数调用无效参数处理程序,如 参数验证所述。 如果执行允许继续,这些函数返回 NULL 并将 errno 到 ERANGE。

在 C++ 中,使用这些功能由模板超加载简化;超加载可能推断缓冲区长度 (自动不再需要指定范围参数),并且还可以用以较新,安全重复自动替换旧,不安全的功能。 有关更多信息,请参见安全模板重载

一般文本例程映射

TCHAR.H 实例

未定义的_UNICODE & _MBCS

定义的_MBCS

定义的_UNICODE

_getts

gets_s

gets_s

_getws

要求

实例

必需的标头

gets_s

<stdio.h>

_getws

<stdio.h> 或 <wchar.h>

控件个在 Windows 应用商店 apps 不受支持。 标准流处理与控件个,stdin,stdout和 stderr,在 C 运行时函数在 Windows 应用商店 apps 之前,可以使用它们必须重定向。 有关其他的兼容性信息,请参见中介绍的 兼容性

示例

// 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 );
}
  

.NET Framework 等效项

System::Console::Read

请参见

参考

流I/O

gets, _getws

fgets, fgetws

fputs, fputws

puts, _putws