Name
cgets - Reads a string from the console.
Syntax
#include <conio.h>
char *cgets(str)
char *str;
Description
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 str. The str 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 terminating null character (\0), and two
additional bytes.
The cgets function continues to read characters until a
carriage-return/line-feed combination (CR-LF) 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, str[1].
Return Value
The cgets function returns a pointer to the start of the
string, which is at str[2]. There is no error return.
See Also
getch(DOS), getche(DOS)
Example
#include <conio.h> #include <stdio.h>
char buffer[82]; char *result;
main( )
{
/* Maximum number of characters */
buffer[0] = 80;
printf("Input line of text, followed by carriage
return:\n ");
/* Input a line of text */
result = cgets(buffer);
printf("\nLine length = %d\nText = %s\n",
buffer[1], result);
} /* "buffer[1]" contains the length. ** "result" points
to the start of the string */
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.
(printed 6/18/89)