Format
#include <conio.h> char *_cgets(char *str);
Language Level: Extension
_cgets reads a string of characters directly from
the keyboard and stores the string and its length in the location
pointed to by str.
_cgets continues to read characters until it meets a carriage return followed by a line feed (CR-LF) or until it reads the specified number of characters. It stores the string starting at str[2]. If _cgets reads a CR-LF combination, it replaces this combination with a null character ('\0') before storing the string. _cgets then stores the actual length of the string in the second array element, str[1].
The str variable must be a pointer to a character array. The first element of the array, str[0], must contain the maximum length, in characters, of the string to be read. The array must have enough elements to hold the string, a final null character, and 2 additional bytes.
Return Value
If successful, _cgets returns a pointer
to the actual start of the string, str[2].
Otherwise, _cgets returns NULL.
Example
This example creates a buffer and
initializes the first byte to the size of the buffer. The program
then accepts an input string using _cgets and displays the size
and text of that string.
#include <conio.h> #include <stdio.h>
void nothing(void)
{
}
int main(void)
{
char buffer[82] = { 84,0 };
char *buffer2;
int i;
_cputs("\nPress any key to continue.");
printf("\n");
while (0 == _kbhit()) {
nothing();
}
_getch();
_cputs("\nEnter a line of text:");
printf("\n");
buffer2 = _cgets(buffer);
printf("\nText entered was: %s", buffer2);
return 0;
/**********************************************
The output should be similar to:
Press any key to continue.
Enter a line of text:
This is a simple test.
Text entered was: This is a simple test.
**********************************************/
}
![]()
_cputs -- Write String to
Screen
fgets -- Read a
String
gets -- Read a
Line
_getch - _getche
-- Read Character from Keyboard
<conio.h>