Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ () — Coherent 3.1.0

Media Vault

Software Library

Restoration Projects

Artifacts Sought


gets()                    STDIO Function                   gets()




Read string from standard input

#include <stdio.h>
char *gets(buffer) char *buffer;

gets  reads characters  from  the standard  input  into a  buffer
pointed at by  buffer.  It stops reading as soon  as it detects a
newline character or EOF.   gets discards the newline or EOF, ap-
pends a null character onto  the string it has built, and returns
another copy of buffer.

***** Example *****

The following example uses gets to get a string from the console;
the string is echoed twice to demonstrate what gets returns.


#include <stdio.h>

main()
{
        char buffer[80];



        printf("Type something: ");



        /*
         * because of the way COHERENT's teletype
         * driver works, the following fflush has
         * no effect.  It should be included for
         * portability to other operating systems.
         */



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


***** See Also *****

buffer, fgets(), getc(), STDIO

***** Diagnostics *****

gets returns NULL if an error occurs or if EOF is seen before any
characters are read.





COHERENT Lexicon                                           Page 1



gets()                    STDIO Function                   gets()



***** Notes *****

gets  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, gets will read it and im-
mediately stop accepting  characters; to the user, it will appear
as if gets is not working at all.

For example, if getchar  is followed by gets, the first character
gets  will  receive  is  the  newline  character left  behind  by
getchar.  A simple statement will remedy this:


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


This throws  away the newline  character left behind  by getchar;
gets will now work correctly.






































COHERENT Lexicon                                           Page 2


Typewritten Software • bear@typewritten.org • Edmonds, WA 98026