Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Turbo C - <b>cgets() get a character string from the console</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
cgets()                  Get a Character String from the Console

 #include <conio.h>                      Required for declarations only

 char       *cgets(string);
 char       *string;                     Storage location for data

    cgets() reads a string from the console and stores it in 'string';
    'string' must be a pointer to a character array. Before calling,
    'string[0]' should be set to the length (in bytes) of the storage
    space available for the string. Upon return, 'string[1]' will contain
    the actual length of the string.  The string is stored starting at
    'string[2]'.

    cgets() reads characters until a carriage-return-line-feed is read,
    or the maximum number of characters (as specified at string[0]) have
    been read. If a (CR-LF) character is read, it is replaced with a null
    character ('\0') before being stored.

    Returns:    A pointer to the start of the string at string[2].  There
                is no error return.

      Notes:    If string[0] is not set by the user to the maximum number
                of characters allowed, the routine reads 0 characters
                from the console and returns. The length of the string
                stored at string[0] should account for the terminating
                null byte, plus the two extra bytes at string[0] and
                string[1].

  -------------------------------- Example ---------------------------------

    The following statements prompt the user for input, then print out
    the string entered and its length.


        #include <conio.h>
        #include <stdio.h>        /* for printf */

        char buffer[100];         /* Can hold 97 chars plus a newline */
        char *name;
        int numread;

        main()
        {
            *buffer = 98;
            printf("Enter your name: ");
            name = cgets(buffer);
            printf("\nName entered: %s \n",name);
            printf("# of chars read: %d\n",buffer[1]);
               /* Number read does not include carriage return,*/
               /* which was replaced by a null byte. */
        }

See Also: getch() getche()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson