Share via


_cgets

Gets a character string from the console.

char*_cgets(char*buffer);

Routine Required Header Compatibility
_cgets <conio.h> Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

_cgets returns a pointer to the start of the string, at buffer[2]. There is no error return.

Parameter

buffer

Storage location for data

Remarks

The _cgets function reads a string of characters from the console and stores the string and its length in the location pointed to by buffer. The buffer parameter must be a pointer to a character array. The first element of the array, buffer[0], must contain the maximum length (in characters) of the string to be read. The array must contain enough elements to hold the string, a terminating null character ('\0'), and two additional bytes. The function reads characters until a carriage-return – linefeed (CR-LF) combination or the specified number of characters is read. The string is stored starting at buffer[2]. If the function reads a CR-LF, it stores the null character ('\0'). _cgets then stores the actual length of the string in the second array element, buffer [1]. Because all editing keys are active when _cgets is called, pressing F3 repeats the last entry.

Example

/* CGETS.C: This program creates a buffer and initializes
 * the first byte to the size of the buffer: 2. Next, the
 * program accepts an input string using _cgets and displays
 * the size and text of that string.
 */

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

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

   printf( "Input line of text, followed by carriage return:\n");
   result = _cgets( buffer );  /* Input a line of text */
   printf( "\nLine length = %d\nText = %s\n", buffer[1], result );
}

Output

Input line of text, followed by carriage return:
This is a line of text

Line length = 22
Text = This is a line of text.

Console and Port I/O Routines

See Also   _getch