fgets

Description

Gets a string from a stream.

#include <stdio.h>

char *fgets( char *string, intn, FILE *stream );

string Storage location for data  
n Number of characters stored  
stream Pointer to FILE structure  

Remarks

The fgets function reads a string from the input stream argument and stores it in string. Characters are read from the current stream position up to and including the first newline character ('\n'), up to the end of the stream, or until the number of characters read is equal to n –1, whichever comes first. The result is stored in string, and a null character ('\0') is appended. The newline character, if read, is included in the string. If n is equal to 1, string is empty (""). The fgets function is similar to the gets function; however, gets replaces the newline character with NULL.

Return Value

If successful, the fgets function returns string. It returns NULL to indicate either an error or end-of-file condition. Use feof or ferror to determine whether an error occurred.

Compatibility

Standards:ANSI, UNIX

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

See Also

fputs, gets, puts

Example

/* FGETS.C: This program uses fgets to display a line from a file on the

* screen.

*/

#include <stdio.h>

FILE *stream;

void main( void )

{

char line[100], *result;

if( (stream = fopen( "fgets.c", "r" )) != NULL )

{

if( fgets( line, 100, stream ) == NULL)

printf( "fgets error\n" );

else

printf( "%s", line);

fclose( stream );

}

}

Output

/* FGETS.C: This program uses fgets to display a line from a file on the