_cgets_cgetws

从控制台获取字符串。 提供这些函数的更安全版本;请参阅 _cgets_s_cgetws_s

重要

这些函数已过时。 从 Visual Studio 2015 开始,CRT 中不再提供这些函数。 这些函数的安全版本 _cgets_s 和 _cgetws_s 仍然可用。 有关这些备用函数的信息,请参阅 _cgets_s_cgetws_s

重要

此 API 不能用于在 Windows 运行时中执行的应用程序。 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数

语法

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

参数

buffer
数据的存储位置。

返回值

_cgets_cgetwsbuffer[2]返回指向字符串起始位置的指针。 如果 bufferNULL,这些函数则会调用无效的参数处理程序,如参数验证。 如果允许继续执行,这些函数则返回 NULL,并将 errno 设置为 EINVAL

备注

这些函数从控制台读取字符构成的字符串,并将该字符串及其长度存储在 buffer指向的位置。 buffer 参数必须是指向字符数组的指针。 数组的第一个元素 buffer[0]必须包含要读取的字符串的最大长度(以字符为单位)。 该数组必须包含足够的元素,以保存该字符串、终止 null 字符 ('\0') 和 2 个其他字节。 此函数会读取字符,直至读取回车符 - 换行符 (CR-LF) 组合或指定的字符数。 字符串以开头 buffer[2]进行存储。 如果此函数读取 CR-LF,它会存储 null 字符 ('\0')。 然后,此函数会在第二个数组元素 buffer[1]中存储字符串的实际长度。

因为处于控制台窗口中时,如果调用 _cgets_cgetws ,所有编辑键都会处于活动状态,所以按 F3 键可以重复最后一个输入的项。

在 C++ 中,这些函数具有模板重载,以调用这些函数的更新、更安全副本。 有关详细信息,请参阅安全模板重载

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

一般文本例程映射

Tchar.h 例程 _UNICODE_MBCS 未定义 _MBCS 已定义 _UNICODE 已定义
_cgetts _cgets _cgets _cgetws

要求

例程 必需的标头
_cgets <conio.h>
_cgetws <conio.h> 或 <wchar.h>

有关兼容性的详细信息,请参阅 兼容性

示例

// crt_cgets.c
// compile with: /c /W3
// This program creates a buffer and initializes
// the first byte to the size of the buffer. Next, the
// program accepts an input string using _cgets and displays
// the size and text of that string.

#include <conio.h>
#include <stdio.h>
#include <errno.h>

int main( void )
{
   char buffer[83] = { 80 };  // Maximum characters in 1st byte
   char *result;

   printf( "Input line of text, followed by carriage return:\n");

   // Input a line of text:
   result = _cgets( buffer ); // C4996
   // Note: _cgets is deprecated; consider using _cgets_s
   if (!result)
   {
      printf( "An error occurred reading from the console:"
              " error code %d\n", errno);
   }
   else
   {
      printf( "\nLine length = %d\nText = %s\n",
              buffer[1], result );
   }
}

      A line of input.Input line of text, followed by carriage return:
Line Length = 16
Text = A line of input.

另请参阅

控制台和端口 I/O
_getch_getwch