Gets a character string from the console.
#include <conio.h> | Required only for function declarations |
char *_cgets( char *buffer );
buffer | Storage location for data |
The _cgets function reads a string of characters directly from the console and stores the string and its length in the location pointed to by buffer. The buffer argument 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 _cgets function continues to read characters until a carriage-return–line-feed (CR-LF) combination is read, or the specified number of characters is read. The string is stored starting at str[2]. If a CR-LF combination is read, it is replaced with a null character ('\0') before being stored. The _cgets function then stores the actual length of the string in the second array element, buffer[1].
Because all DOS editing keys are active when you call _cgets, pressing F3 repeats the last entry.
The _cgets function returns a pointer to the start of the string, at buffer[2]. There is no error return.
Standards:None
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
/* 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 first 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 );
}
Input line of text, followed by carriage return:
This is some text
Line length = 17
Text = This is some text