Reads string from console
Declaration:
char *cgets(char *str);
Remarks:
cgets reads a string of characters from the console and stores the string
(and the string length) in the location *str.
Before you call cgets, set str[0] to the maximum length of the string to be
read.
cgets reads characters until it encounters a carriage-return/linefeed
combination (CR/LF), or until the maximum allowable number of characters
have been read.
If cgets reads a CR/LF, it replaces the CR/LF with a \0 (null terminator)
before storing the string.
On return, str[1] is set to the number of characters actually read.
The characters read start at str[2] and end with a null terminator, so str
must be at least (str[0] + 2) bytes long.
Return Value:
On success, returns a pointer to str[2].
Example:
#include <stdio.h>
#include <conio.h>
int main(void)
{
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[1], 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[1], p);
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
return 0;
}
0 comments:
Post a Comment