gets()
PROTOTYPE
#include <stdio.h>
char *gets(char *str);
DESCRIPTION
gets() reads bytes from the current position of the open regular file specified by stdin, advances that position, and writes to the buffer specified by str until end-of-file is reached, an error occurs, or a newline character is read. If encountered, newline is discarded. If stdin is at end-of-file, the end-of-file indicator is set.
Unless disabled via FSF_NOATIME or only a byte pushed back by ungetc() is read, stdin’s data access timestamp is updated.
Because it cannot check whether the provided buffer is big enough to hold the number of bytes read, gets() is dangerous. Use fgets() instead.
If end of file is reached before any bytes are read, gets() returns NULL without modifying the output buffer. Otherwise, the string is terminated with a null character. If an error occurs, gets() sets errno and stdin’s error indicator, and returns NULL. Otherwise, gets() returns str.
ERROR CODES
| EBADF | stdin is not the handle of a file open in read mode. |
| EFAULT | str equals NULL. |
| EISDIR | stdin refers to a directory. |
EXAMPLE
char s[HUGE_BUFFER];
/*-----------------------------------------------------------------*/
/* Read string from stdin. */
/*-----------------------------------------------------------------*/
if (gets(s) == NULL)
{
perror("gets() failed");
return -1;
}