Warning: main(/www/www/htdocs/style/globals.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/g/gets.html on line 1
Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/globals.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/g/gets.html on line 1
Warning: main(/www/www/htdocs/style/header.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/g/gets.html on line 8
Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/header.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/g/gets.html on line 8
Get a string of characters from standard input
#include <stdio.h>
char *gets( char *buf );
- buf
- A buffer where the function can store the string.
libc
Use the -l c option to
qcc
to link against this library.
This library is usually included automatically.
The gets() function gets a string of characters from the
stdin stream, and stores them in the array pointed to by
buf until end-of-file is encountered or a newline character is
read. Any newline character is discarded, and the string is
NUL-terminated.
|
You should use
fgets() instead of
gets(); gets() happily overflows the
buf array if a newline character isn't read from
stdin before the end of the array is reached. |
The gets() function is similar to
fgets(), except that
gets() operates with stdin, has no size argument,
and replaces
a newline character with the NUL character.
A pointer to buf, or
NULL when end-of-file is encountered before reading any
characters or a read error occurred
(errno is set).
|
Use
feof()
or
ferror()
to distinguish an end-of-file condition from an error. |
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
char buffer[80];
while( gets( buffer ) != NULL ) {
puts( buffer );
}
return EXIT_SUCCESS;
}
ANSI,
POSIX 1003.1
Safety: | |
Cancellation point |
Yes |
Interrupt handler |
No |
Signal handler |
No |
Thread |
Yes |
errno,
feof(),
ferror(),
fopen(),
getc(),
fgetc(),
fgets(),
puts(),
ungetc()
Warning: main(/www/www/htdocs/style/footer.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/g/gets.html on line 176
Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/footer.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/g/gets.html on line 176