_cgets, _cgetws

从控件获取个字符字符串。 这些功能的更安全版本可用;请参见 _cgets_s, _cgetws_s

重要

此 API 不能在运行时的窗口执行的应用程序。有关更多信息,请参见 CRT 函数不支持与 /ZW

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 和 _cgetws 返回指向该字符串的开头,在 buffer[2]。 如果 buffer 是 NULL,这些函数调用的参数无效处理程序,如 参数验证所述。 如果执行允许继续,它们返回 NULL 并将 errno 到 EINVAL。

备注

这些函数读取字符串从控件个并在位置存储该字符串及其长度指向由 buffer。 buffer 参数必须是指向字符数组。 数组,buffer[0]的第一个元素,必须包含最大长度 (以字符要读取的) 的该字符串。 数组必须有足够的元素包含该字符串、一个终止 null 字符 (“\0 ") 和 2 个附加的字节。 函数读取直到支持返回换行符 (CR-LF) 组合的字符或字符指定数目的读取。 该字符串是存储的开始。buffer[2]。 如果函数读取 CR-LF,它存储在 null 字符 (“\0 ")。 函数在第二个数组元素,buffer[1]然后将该字符串的实际长度。

由于所有编辑键处于活动状态,当 _cgets 或_cgetws 在控件个窗口调用,如果为,则按个 F3 键重复上一次输入的项时。

在 C++ 中,这些函数的调用的模板超负载越 + 新,保证这些函数副本。 有关更多信息,请参见安全模板重载

一般文本例程映射

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

请参见

参考

控制台和端口I/O

_getch, _getwch