cgets
Reads a string from the console.
Defined in header <conio.h>
Syntax
#include <conio.h>
char *cgets(char *str);
Portability
| DOS | UNIX | Windows | ANSI C | C++ only |
|---|---|---|---|---|
| ■ |
Remarks
cgets reads a string of characters from the console, storing the string (and the string length) in the location pointed to by str.
cgets reads characters until it encounters a carriage-return/linefeed
| (CR/LF) combination, or until the maximum | allowable number of |
characters have been read. If cgets reads a carriage return/linefeed combination, it replaces the combination with a \0 (null terminator) before storing the string.
| Before cgets is called, set str[0] to the maximum | length of the string to be |
read. On return, str[l] is set to the number of characters actually read. The characters read start at str[2] and end with a null terminator. Thus, str must be at least str[0] plus 2 bytes long.
Return value
On success, cgets returns a pointer to str[2].
See also
Example
#include <stdio.h>
#include <conio.h>
mainO
{
char buffer[83];
char *p;
/* there's space for 80 characters plus the NULL terminator */
buffer[0] = 81;
printf ("Input some chars:");
p = cgets (buffer);
printf ("\ncgets read %d characters \"%s\"\n", buffer[l], p) ;
printf ("The returned pointer is %p buffer [0] is at %p\n", p, &buffer)
/* leave room for 5 characters plus the NULL terminator *
buffer [0] = 6;
printf ("Input some chars:");
p = cgets (buffer);
printf ("\ncgets read %d characters: \"%s\"\n", buffer[l], P)
printf ("The returned pointer is %p, buffer [0] is at lp\n" p, &buffer)
return 0;
Differences from modern implementations
Nothing recorded yet.
Modern references
These are search links, not yet checked by hand.
Stable link: /3.1/conio.h/cgets/
· short form /3.1/cgets/