gets(3S) gets(3S)
NAME
gets, fgets - get a string from a stream
SYNOPSIS
#include <stdio.h>
char *gets(char *s);
char *fgets(char *s, int n, FILE *stream);
DESCRIPTION
gets() reads bytes from standard input stdin into the array pointed to
by s, until a newline character is read or an end-of-file condition is
encountered. The newline character is discarded and the string is ter-
minated with a null character.
fgets() reads bytes from the stream into the array pointed to by s,
until n-1 bytes are read, or a newline character is read and trans-
ferred to s, or an end-of-file condition is encountered. The string is
then terminated with a null character.
When using gets(), if the length of an input line exceeds the size of
s, indeterminate behavior may result. fgets() should therefore be used
instead of gets().
ERRORS
The following error code descriptions are function-specific. You will
find a general description in introprm2(2) or in errno(5).
The functions will fail if:
EAGAIN The ONONBLOCK flag is set for the file descriptor under-
lying stream and the process would be delayed in the
operation.
EBADF The file descriptor underlying stream is not a valid file
descriptor open for reading.
EINTR The read operation was terminated due to the receipt of a
signal, and no data was transferred.
EIO A physical I/O error has occurred, or the process is in a
background process group attempting to read from its con-
trolling terminal, and either the process is ignoring or
blocking the SIGTTIN signal or the process group is
orphaned. This error may also be generated for implementa-
tion-dependent reasons.
EOVERFLOW The file is a regular file and an attempt was made to read
at or beyond the offset maximum associated with the corre-
sponding stream.
Page 1 Reliant UNIX 5.44 Printed 11/98
gets(3S) gets(3S)
The functions may fail if:
ENOMEM Insufficient storage space is available.
ENXIO A request was made of a non-existent device, or the
request was outside the capabilities of the device.
RESULT
If end-of-file is encountered and no bytes have been read, no bytes
are transferred to s and a null pointer is returned. If a read error
occurs, such as trying to use these functions on a file that has not
been opened for reading, a null pointer is returned and the error
indicator for the stream is set. If end-of-file is encountered, the
EOF indicator for the stream is set. Otherwise s is returned.
SEE ALSO
lseek(2), read(2), ferror(3S), fopen(3S), fread(3S), getc(3S),
scanf(3S), stdio(3S), ungetc(3S), lfs(5), stdio(5).
Page 2 Reliant UNIX 5.44 Printed 11/98