1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
.\" 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
.\"
.\" 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.
.\"
.\" 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)
.\" Translated into Spanish Tue Feb 3 1998 by Gerardo Aburruzaga
. \" García <gerardo.aburruzaga@uca.es>
.\"
.TH GETS 3 "4 abril 1993" "GNU" "Manual del Programador de Linux"
.SH NOMBRE
fgetc, fgets, getc, getchar, gets, ungetc \- entrada de caracteres y
cadenas de ellos
.SH SINOPSIS
.nf
.B #include <stdio.h>
.sp
.BI "int fgetc(FILE *" flujo ");"
.nl
.BI "char *fgets(char *" "s" ", int " "tam" ", FILE *" "flujo" ");"
.nl
.BI "int getc(FILE *" flujo ");"
.nl
.BI "int getchar(void);"
.nl
.BI "char *gets(char *" "s" ");"
.nl
.BI "int ungetc(int " c ", FILE *" flujo ");"
.SH DESCRIPCIÓN
.B fgetc()
lee el siguiente carácter de
.I flujo
y lo devuelve como un
.B unsigned char
modelado a un
.BR int ,
o
.B EOF
al llegar al final del flujo o en caso de error.
.PP
.B getc()
es equivalente a
.B fgetc()
excepto en el hecho de que puede estar implementado como una macro que
evalúe
.I flujo
más de una vez.
.PP
.B getchar()
es equivalente a
.BI "getc(" stdin ) \fR.
.PP
.BR gets() " lee"
una línea de
.I stdin
y la guarda en el búfer al que apunta
.I s
hasta que se encuentre bien un carácter terminador nueva-línea, bien
.BR EOF ,
al cual reemplaza con
.BR '\e0' .
No se hace ninguna comprobación de desbordamiento del búfer (vea
.B FALLOS
más abajo).
.PP
.B fgets()
lee como mucho uno menos de
.I tam
caracteres del
.I flujo
y los guarda en el búfer al que apunte
.IR s .
La lectura se para tras un
.B EOF
o una nueva-línea. Si se lee una nueva-línea, se guarda en el búfer. Tras
el último carácter en el búfer se guarda un
.BR '\e0' .
.PP
.B ungetc()
pone
.I c
de vuelta en
.IR flujo ,
modelado a
.BR "unsigned char" ,
donde queda disponible para una posterior operación de lectura. Los
caracteres puestos en el flujo serán devueltos en orden inverso; sólo
se garantiza que se pueda devolver al flujo un carácter.
.PP
Las llamadas a las funciones descritas aquí pueden mezclarse unas con
otras y con llamadas a otras funciones de entrada de la biblioteca
.B stdio
para el mismo flujo de entrada.
.PP
Para las versiones no-bloqueantes, véase
.BR unlocked_stdio (3).
.SH "VALOR DEVUELTO"
.BR fgetc() , " getc() " y " getchar()"
devuelven el carácter leído como un
.B unsigned char
modelado a un
.B int
o
.B EOF
al llegar al final de la entrada o en caso de error.
.PP
.BR gets() " y " fgets()
devuelven
.I s
en caso de éxito, y
.B NULL
en caso de error o cuando se llegue al final del fichero mientras que
no se haya leído ningún carácter.
.PP
.B ungetc()
devuelve
.I c
en caso de éxito, o
.B EOF
en caso de error.
.SH "CONFORME A"
ANSI - C, POSIX.1
.SH "FALLOS"
No use nunca
.BR gets() .
Puesto que es imposible saber, sin conocer de antemano los datos,
cuántos caracteres va a leer
.BR gets() ,
y puesto que
.B gets()
continuará guardando caracteres una vez alcanzado el final del búfer,
su empleo es extremadamente peligroso. Muchas veces ha sido utilizado
para comprometer la seguridad de un sistema. En su lugar emplee
.B fgets()
siempre que pueda.
.PP
No es recomendable mezclar llamadas a las funciones de entrada de la biblioteca
.B stdio
con llamadas de bajo nivel a
.B read()
para el descriptor de fichero asociado con el flujo de entrada; los
resultados serán indefinidos y probablemente no los deseados.
.SH "VÉASE TAMBIÉN"
.BR read (2),
.BR write (2),
.BR ferror (3),
.BR fopen (3),
.BR fread (3),
.BR fseek (3),
.BR puts (3),
.BR scanf (3),
.BR unlocked_stdio (3)
|