Export (0) Print
Expand All
div
Expand Minimize

_cgets, _cgetws

Get a character string from the console.

char *_cgets( 
   char *buffer 
);
wchar_t *_cgetws(
   wchar_t *buffer
);

Parameter

buffer
Storage location for data.

Return Value

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

Remarks

These functions read a string of characters from the console and store 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–line feed (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'). The function then stores the actual length of the string in the second array element, buffer[1]. Because all editing keys are active when _cgets or _cgetws is called, pressing F3 repeats the last entry.

Generic-Text Routine Mappings

TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
_cgetts _cgets _cgets _cgetws

Requirements

Routine Required header Compatibility
_cgets <conio.h> Win 98, Win Me, Win NT, Win 2000, Win XP
_cgetws <conio.h> or <wchar.h> Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

Example

// crt_cgets.c
// compile with: /c
/* 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>

int main( void )
{
   char buffer[83] = { 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 );
}

Sample 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

See Also

Console and Port I/O Routines | _getch | Run-Time Routines and .NET Framework Equivalents

Show:
© 2014 Microsoft