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/f/fgets.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/f/fgets.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/f/fgets.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/f/fgets.html on line 8
Read a string of characters from a stream
#include <stdio.h>
char* fgets( char* buf,
size_t n,
FILE* fp );
- buf
- A pointer to a buffer in which fgets() can store the
characters that it reads.
- n
- The maximum number of characters to read.
- fp
- The stream from which to read the characters.
libc
Use the -l c option to
qcc
to link against this library.
This library is usually included automatically.
The fgets() function reads a string of characters from the stream
specified by fp, and stores them in the array specified by
buf.
It stops reading characters when:
- the end-of-file is reached
Or:
- a newline ('\n') character is read
Or:
- n-1 characters have been read.
The newline character isn't discarded.
A null character is placed immediately after the last character
read into the array.
|
Don't assume that there's a newline character in every string that you
read with fgets().
A newline character isn't present if there are more than
n-1 characters before the newline.
Also, a newline character might not appear as the last character in a
file when the end-of-file is reached. |
The same pointer as buf,
or NULL if the stream is at
the end-of-file or an error occurs
(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 )
{
FILE *fp;
char buffer[80];
fp = fopen( "file", "r" );
if( fp != NULL ) {
while( fgets( buffer, 80, fp ) != NULL ) {
fputs( buffer, stdout );
}
fclose( fp );
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
ANSI,
POSIX 1003.1
Safety: | |
Cancellation point |
Yes |
Interrupt handler |
No |
Signal handler |
No |
Thread |
Yes |
errno,
feof(),
ferror(),
fopen(),
fputs(),
getc(),
gets(),
fgetc()
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/f/fgets.html on line 204
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/f/fgets.html on line 204