fgets

 

Description

Reads a string from the input stream argument and stores it in string.

Syntax

char * fgets( char * str, int nLen, FILE * stream )

Parameters

str
Storage location for data
nLen
Maximum number of characters to read
stream
Pointer to FILE

Return

It returns string. NULL is returned to indicate an error or an end-of-file condition. Use feof or ferror to determine whether an error occurred.

Examples

EX1

//The following example uses fgets to display a line from a file and then print out this line

void test_fgets()
{
   FILE *stream;
   char line[100];
   stream = fopen( __FILE__, "r" );
   if(stream != NULL )
   {
      if( fgets( line, 100, stream ) == NULL)
         printf( "fgets error\n" );
      else
         printf( "%s", line);
      fclose( stream );
   }
}

Remark

fgets reads a string from the input stream argument and stores it in string.

fgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n-1, whichever comes first. The result stored in string is appended with a null character. The newline character, if read, is included in the string.

See Also

fputs

Header to Include

origin.h

Reference