2004-11-03 13:51:07 +00:00
|
|
|
.\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to make and distribute verbatim copies of this
|
|
|
|
.\" manual provided the copyright notice and this permission notice are
|
|
|
|
.\" preserved on all copies.
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to copy and distribute modified versions of this
|
|
|
|
.\" manual under the conditions for verbatim copying, provided that the
|
|
|
|
.\" entire resulting derived work is distributed under the terms of a
|
|
|
|
.\" permission notice identical to this one.
|
2007-04-12 22:42:49 +00:00
|
|
|
.\"
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" Since the Linux kernel and libraries are constantly changing, this
|
|
|
|
.\" manual page may be incorrect or out-of-date. The author(s) assume no
|
|
|
|
.\" responsibility for errors or omissions, or for damages resulting from
|
|
|
|
.\" the use of the information contained herein. The author(s) may not
|
|
|
|
.\" have taken the same level of care in the production of this manual,
|
|
|
|
.\" which is licensed free of charge, as they might when working
|
|
|
|
.\" professionally.
|
2007-04-12 22:42:49 +00:00
|
|
|
.\"
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" Formatted or processed versions of this manual, if unaccompanied by
|
|
|
|
.\" the source, must acknowledge the copyright and authors of this work.
|
|
|
|
.\" License.
|
|
|
|
.\" Modified Wed Jul 28 11:12:07 1993 by Rik Faith (faith@cs.unc.edu)
|
|
|
|
.\" Modified Fri Sep 8 15:48:13 1995 by Andries Brouwer (aeb@cwi.nl)
|
gets.3, mbtowc.3, printf.3, puts.3, scanf.3, stpcpy.3, stpncpy.3, strcat.3, strlen.3, strnlen.3, strsep.3, strstr.3, strxfrm.3, utmp.5: Global fix: consistent use of "null byte"
Bring more consistency to the discussion of
"[terminating] null byte"
by writing (at least in the initial use in a page)
"[terminating] null byte ('\0')".
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2011-09-28 04:16:17 +00:00
|
|
|
.TH GETS 3 2011-09-28 "GNU" "Linux Programmer's Manual"
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH NAME
|
|
|
|
fgetc, fgets, getc, getchar, gets, ungetc \- input of characters and strings
|
|
|
|
.SH SYNOPSIS
|
|
|
|
.nf
|
|
|
|
.B #include <stdio.h>
|
|
|
|
.sp
|
|
|
|
.BI "int fgetc(FILE *" stream );
|
2007-12-23 13:45:24 +00:00
|
|
|
|
2004-11-03 13:51:07 +00:00
|
|
|
.BI "char *fgets(char *" "s" ", int " "size" ", FILE *" "stream" );
|
2007-12-23 13:45:24 +00:00
|
|
|
|
2004-11-03 13:51:07 +00:00
|
|
|
.BI "int getc(FILE *" stream );
|
2007-12-23 13:45:24 +00:00
|
|
|
|
2007-09-20 16:26:31 +00:00
|
|
|
.B "int getchar(void);"
|
2007-12-23 13:45:24 +00:00
|
|
|
|
2004-11-03 13:51:07 +00:00
|
|
|
.BI "char *gets(char *" "s" );
|
2007-12-23 13:45:24 +00:00
|
|
|
|
2004-11-03 13:51:07 +00:00
|
|
|
.BI "int ungetc(int " c ", FILE *" stream );
|
2007-12-23 21:05:57 +00:00
|
|
|
.fi
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH DESCRIPTION
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR fgetc ()
|
2004-11-03 13:51:07 +00:00
|
|
|
reads the next character from
|
2007-04-12 22:42:49 +00:00
|
|
|
.I stream
|
2004-11-03 13:51:07 +00:00
|
|
|
and returns it as an
|
2006-02-09 20:24:53 +00:00
|
|
|
.I unsigned char
|
2004-11-03 13:51:07 +00:00
|
|
|
cast to an
|
2006-02-09 20:24:53 +00:00
|
|
|
.IR int ,
|
2004-11-03 13:51:07 +00:00
|
|
|
or
|
|
|
|
.B EOF
|
|
|
|
on end of file or error.
|
|
|
|
.PP
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR getc ()
|
2004-11-03 13:51:07 +00:00
|
|
|
is equivalent to
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR fgetc ()
|
2004-11-03 13:51:07 +00:00
|
|
|
except that it may be implemented as a macro which evaluates
|
|
|
|
.I stream
|
|
|
|
more than once.
|
|
|
|
.PP
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR getchar ()
|
2004-11-03 13:51:07 +00:00
|
|
|
is equivalent to
|
|
|
|
.BI "getc(" stdin ) \fR.
|
|
|
|
.PP
|
2007-04-12 22:42:49 +00:00
|
|
|
.BR gets ()
|
2005-10-19 07:29:28 +00:00
|
|
|
reads a line from
|
2004-11-03 13:51:07 +00:00
|
|
|
.I stdin
|
|
|
|
into the buffer pointed to by
|
|
|
|
.I s
|
|
|
|
until either a terminating newline or
|
|
|
|
.BR EOF ,
|
gets.3, mbtowc.3, printf.3, puts.3, scanf.3, stpcpy.3, stpncpy.3, strcat.3, strlen.3, strnlen.3, strsep.3, strstr.3, strxfrm.3, utmp.5: Global fix: consistent use of "null byte"
Bring more consistency to the discussion of
"[terminating] null byte"
by writing (at least in the initial use in a page)
"[terminating] null byte ('\0')".
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2011-09-28 04:16:17 +00:00
|
|
|
which it replaces with a null byte (\(aq\e0\(aq).
|
2007-12-10 06:42:35 +00:00
|
|
|
No check for buffer overrun is performed (see BUGS below).
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR fgets ()
|
2004-11-03 13:51:07 +00:00
|
|
|
reads in at most one less than
|
|
|
|
.I size
|
|
|
|
characters from
|
|
|
|
.I stream
|
|
|
|
and stores them into the buffer pointed to by
|
|
|
|
.IR s .
|
|
|
|
Reading stops after an
|
|
|
|
.B EOF
|
2007-04-12 22:42:49 +00:00
|
|
|
or a newline.
|
|
|
|
If a newline is read, it is stored into the buffer.
|
gets.3, mbtowc.3, printf.3, puts.3, scanf.3, stpcpy.3, stpncpy.3, strcat.3, strlen.3, strnlen.3, strsep.3, strstr.3, strxfrm.3, utmp.5: Global fix: consistent use of "null byte"
Bring more consistency to the discussion of
"[terminating] null byte"
by writing (at least in the initial use in a page)
"[terminating] null byte ('\0')".
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2011-09-28 04:16:17 +00:00
|
|
|
A terminating null byte (\(aq\e0\(aq)
|
2004-11-03 13:51:07 +00:00
|
|
|
is stored after the last character in the buffer.
|
|
|
|
.PP
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR ungetc ()
|
2004-11-03 13:51:07 +00:00
|
|
|
pushes
|
|
|
|
.I c
|
|
|
|
back to
|
|
|
|
.IR stream ,
|
|
|
|
cast to
|
2006-02-09 20:24:53 +00:00
|
|
|
.IR "unsigned char" ,
|
2007-04-12 22:42:49 +00:00
|
|
|
where it is available for subsequent read operations.
|
|
|
|
Pushed-back characters
|
2004-11-03 13:51:07 +00:00
|
|
|
will be returned in reverse order; only one pushback is guaranteed.
|
|
|
|
.PP
|
|
|
|
Calls to the functions described here can be mixed with each other and with
|
|
|
|
calls to other input functions from the
|
2007-06-23 07:56:56 +00:00
|
|
|
.I stdio
|
2004-11-03 13:51:07 +00:00
|
|
|
library for the same input stream.
|
|
|
|
.PP
|
accept.2, access.2, acct.2, clock_nanosleep.2, mbind.2, mincore.2, remap_file_pages.2, sched_setscheduler.2, set_mempolicy.2, splice.2, stat.2, syslog.2, timer_create.2, timerfd_create.2, truncate.2, fenv.3, ferror.3, fflush.3, fgetwc.3, fgetws.3, flockfile.3, fputwc.3, fputws.3, fread.3, getopt.3, gets.3, getwchar.3, glob.3, iconv.3, longjmp.3, pow.3, printf.3, puts.3, putwchar.3, regex.3, rpc.3, scanf.3, setjmp.3, termios.3, unlocked_stdio.3, wcswidth.3, hd.4, rtc.4, st.4, core.5, dir_colors.5, elf.5, proc.5, arp.7, ascii.7, boot.7, bootparam.7, charsets.7, futex.7, ip.7, iso_8859-11.7, man-pages.7, man.7, mdoc.samples.7, path_resolution.7, pipe.7, posixoptions.7, unicode.7, unix.7, uri.7, utf-8.7, ld.so.8: s/non-/non/
The tendency in English, as prescribed in style guides like
Chicago MoS, is towards removing hyphens after prefixes
like "non-" etc.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-01-16 17:20:12 +00:00
|
|
|
For nonlocking counterparts, see
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR unlocked_stdio (3).
|
|
|
|
.SH "RETURN VALUE"
|
2007-04-12 22:42:49 +00:00
|
|
|
.BR fgetc (),
|
|
|
|
.BR getc ()
|
|
|
|
and
|
2005-10-19 07:29:28 +00:00
|
|
|
.BR getchar ()
|
2004-11-03 13:51:07 +00:00
|
|
|
return the character read as an
|
2006-02-09 20:24:53 +00:00
|
|
|
.I unsigned char
|
2004-11-03 13:51:07 +00:00
|
|
|
cast to an
|
2006-02-09 20:24:53 +00:00
|
|
|
.I int
|
2004-11-03 13:51:07 +00:00
|
|
|
or
|
|
|
|
.B EOF
|
|
|
|
on end of file or error.
|
|
|
|
.PP
|
2007-04-12 22:42:49 +00:00
|
|
|
.BR gets ()
|
|
|
|
and
|
2005-10-19 14:54:31 +00:00
|
|
|
.BR fgets ()
|
2004-11-03 13:51:07 +00:00
|
|
|
return
|
|
|
|
.I s
|
2005-11-02 13:55:25 +00:00
|
|
|
on success, and NULL
|
2004-11-03 13:51:07 +00:00
|
|
|
on error or when end of file occurs while no characters have been read.
|
|
|
|
.PP
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR ungetc ()
|
2007-04-12 22:42:49 +00:00
|
|
|
returns
|
2004-11-03 13:51:07 +00:00
|
|
|
.I c
|
|
|
|
on success, or
|
|
|
|
.B EOF
|
|
|
|
on error.
|
|
|
|
.SH "CONFORMING TO"
|
2008-08-06 12:20:20 +00:00
|
|
|
C89, C99, POSIX.1-2001.
|
2004-11-03 13:51:07 +00:00
|
|
|
LSB deprecates
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR gets ().
|
2008-08-06 12:20:20 +00:00
|
|
|
POSIX.1-2008 removes the specification of
|
|
|
|
.BR gets ().
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH BUGS
|
|
|
|
Never use
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR gets ().
|
2004-11-03 13:51:07 +00:00
|
|
|
Because it is impossible to tell without knowing the data in advance how many
|
|
|
|
characters
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR gets ()
|
2004-11-03 13:51:07 +00:00
|
|
|
will read, and because
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR gets ()
|
2007-04-12 22:42:49 +00:00
|
|
|
will continue to store characters past the end of the buffer,
|
|
|
|
it is extremely dangerous to use.
|
|
|
|
It has been used to break computer security.
|
|
|
|
Use
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR fgets ()
|
2004-11-03 13:51:07 +00:00
|
|
|
instead.
|
|
|
|
.PP
|
|
|
|
It is not advisable to mix calls to input functions from the
|
2007-06-23 07:56:56 +00:00
|
|
|
.I stdio
|
2005-07-06 08:00:30 +00:00
|
|
|
library with low-level calls to
|
2007-06-14 06:25:37 +00:00
|
|
|
.BR read (2)
|
2004-11-03 13:51:07 +00:00
|
|
|
for the file descriptor associated with the input stream; the results
|
|
|
|
will be undefined and very probably not what you want.
|
|
|
|
.SH "SEE ALSO"
|
|
|
|
.BR read (2),
|
|
|
|
.BR write (2),
|
|
|
|
.BR ferror (3),
|
2006-02-09 20:57:44 +00:00
|
|
|
.BR fgetwc (3),
|
2006-03-02 03:11:54 +00:00
|
|
|
.BR fgetws (3),
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR fopen (3),
|
|
|
|
.BR fread (3),
|
|
|
|
.BR fseek (3),
|
2006-04-25 21:31:05 +00:00
|
|
|
.BR getline (3),
|
2006-02-09 20:57:44 +00:00
|
|
|
.BR getwchar (3),
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR puts (3),
|
|
|
|
.BR scanf (3),
|
2006-02-09 20:57:44 +00:00
|
|
|
.BR ungetwc (3),
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR unlocked_stdio (3)
|