ggeettss() -- STDIO Function (libc)

Read string from standard input
#iinncclluuddee <ssttddiioo.hh>
cchhaarr *ggeettss(_b_u_f_f_e_r)
cchhaarr *_b_u_f_f_e_r;

ggeettss() reads characters from the standard input into a buffer pointed at by
_b_u_f_f_e_r.  It stops reading as soon as it detects a newline character or EOF.
ggeettss() discards  the newline  or EOF,  appends NUL onto  the string  it has
built, and returns another copy of _b_u_f_f_e_r.

_E_x_a_m_p_l_e
The following  example uses ggeettss()  to get a  string from the  console; the
string is echoed twice to demonstrate what ggeettss() returns.

#include <stdio.h>

main()
{
    char buffer[80];

    printf("Type something: ");

    fflush(stdout);
    printf("%s\n%s\n", gets(buffer), buffer);
}

_S_e_e _A_l_s_o
bbuuffffeerr, ffggeettss(), ggeettcc(), lliibbcc
ANSI Standard, section 7.9.7.7
POSIX Standard, section 8.1

_D_i_a_g_n_o_s_t_i_c_s
ggeettss()  returns NULL  if  an error  occurs  or if  EOF is  seen before  any
characters are read.

_N_o_t_e_s
ggeettss()  stops reading  the input  string as  soon as  it detects  a newline
character.  If  a previous  input routine left  a newline character  in the
standard input  buffer, ggeettss() will read it  and immediately stop accepting
characters; to the user, it will appear as if ggeettss() is not working at all.

For example, if ggeettcchhaarr() is followed by ggeettss(), the first character ggeettss()
will receive  is the newline character left behind  by ggeettcchhaarr().  A simple
statement will remedy this:

    while (getchar() != '\n')
        ;

This throws  away the  newline character  left behind by  ggeettcchhaarr(); ggeettss()
will now work correctly.
