'\" te .\" Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. .\" Copyright 1989 AT&T. .\" Portions Copyright (c) 1992, X/Open Company Limited. All Rights Reserved. .TH gets 3C "15 Oct 2003" "SunOS 5.11" "Standard C Library Functions" .SH NAME gets, fgets \- get a string from a stream .SH SYNOPSIS .LP .nf #include \fBchar *\fR\fBgets\fR(\fBchar *\fR\fIs\fR); .fi .LP .nf \fBchar *\fR\fBfgets\fR(\fBchar *\fR\fIs\fR, \fBint\fR \fIn\fR, \fBFILE *\fR\fIstream\fR); .fi .SH DESCRIPTION .sp .LP The \fBgets()\fR function reads bytes from the standard input stream (see \fBIntro\fR(3)), \fBstdin\fR, into the array pointed to by \fIs\fR, until a newline character is read or an end-of-file condition is encountered. The newline character is discarded and the string is terminated with a null byte. .sp .LP If the length of an input line exceeds the size of \fIs\fR, indeterminate behavior may result. For this reason, it is strongly recommended that \fBgets()\fR be avoided in favor of \fBfgets()\fR. .sp .LP The \fBfgets()\fR function reads bytes from the \fIstream\fR into the array pointed to by \fIs\fR, until \fIn\fR\(mi1 bytes are read, or a newline character is read and transferred to \fIs\fR, or an end-of-file condition is encountered. The string is then terminated with a null byte. .sp .LP The \fBfgets()\fR and \fBgets()\fR functions may mark the \fBst_atime\fR field of the file associated with \fIstream\fR for update. The \fBst_atime\fR field will be marked for update by the first successful execution of \fBfgetc\fR(3C), \fBfgets()\fR, \fBfread\fR(3C), \fBfscanf\fR(3C), \fBgetc\fR(3C), \fBgetchar\fR(3C), \fBgetdelim\fR(3C), \fBgetline\fR(3C), \fBgets()\fR, or \fBscanf\fR(3C) using \fIstream\fR that returns data not supplied by a prior call to \fBungetc\fR(3C) or \fBungetwc\fR(3C). .SH RETURN VALUES .sp .LP If end-of-file is encountered and no bytes have been read, no bytes are transferred to \fIs\fR and a null pointer is returned. For standard-conforming (see \fBstandards\fR(5)) applications, if the end-of-file indicator for the stream is set, no bytes are transferred to \fIs\fR and a null pointer is returned whether or not the stream is at end-of-file. 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 \fBEOF\fR indicator for the stream is set. Otherwise \fIs\fR is returned. .SH ERRORS .sp .LP Refer to \fBfgetc\fR(3C). .SH ATTRIBUTES .sp .LP See \fBattributes\fR(5) for descriptions of the following attributes: .sp .sp .TS tab() box; cw(2.75i) |cw(2.75i) lw(2.75i) |lw(2.75i) . ATTRIBUTE TYPEATTRIBUTE VALUE _ Interface StabilityCommitted _ MT-LevelMT-Safe _ StandardSee \fBstandards\fR(5). .TE .SH NOTE .sp .LP The use of \fBgets()\fR function is discouraged since the user cannot specify the length of the buffer passed to \fBgets()\fR. The length of the string read is unlimited. It is possible to overflow this buffer in such a way as to cause applications to fail, or possible system security violations. .sp .LP Applications should use the \fBfgets()\fR function instead of the obsolescent \fBgets()\fR function. .sp .LP The ISO C99 standard Technical Corrigendum 3 (ISO/IEC 9899:1999 Cor. 3:2007(E)), the X/Open Portability Guide Issue 7 (XPG7), and the IEEE POSIX 1003.1-2008 standard have all declared the \fBgets()\fR function is obsolescent and deprecated, and may be removed in future versions. .sp .LP The ISO C11 standard (ISO/IEC 9899:2011) does not include \fBgets()\fR. When compiling with __STDC_VERSION__ >= 201112L, which specifies C11 mode, then the \fB\fR header will not provide a function prototype for \fBgets()\fR. .SH SEE ALSO .sp .LP \fBlseek\fR(2), \fBread\fR(2), \fBferror\fR(3C), \fBfgetc\fR(3C), \fBfgetwc\fR(3C), \fBfopen\fR(3C), \fBfread\fR(3C), \fBgetchar\fR(3C), \fBgetdelim\fR(3C), \fBgetline\fR(3C), \fBscanf\fR(3C), \fBstdio\fR(3C), \fBungetc\fR(3C), \fBungetwc\fR(3C), \fBattributes\fR(5), \fBstandards\fR(5)