fgets
char *  fgets (char * string , int num , FILE * stream);
stdio.h
  cplusplus.com  

Get a string from a stream.
  Reads characters from stream and stores them in string until (num -1) characters have been read or a newline or EOF character is reached, whichever comes first.
  A newline character ends reading but is considered a valid character and included in the new string.
  A null character is always appended at the end of the resulting string.

Parameters.

string
pointer to a buffer where to store data read.
num
max number of bytes to be read.
stream
pointer to an open file.

Return Value.
  On success, the string read is returned.
  On end-of-file or error, null pointer is returned. Use ferror or feof to check what happend

Example.

/* fgets exmaple */
#include <stdio.h>

main()
{
   FILE * pFile;
   char string [100];

   pFile = fopen ("myfile.txt" , "r");
   if (pFile == NULL) perror ("Error opening file");
   else {
     fgets (string , 100 , pFile);
     puts (string);
     fclose (pFile);
   }
   return 0;
}
This example reads and prints out on the screen the first line of myfile.txt or the first 100 characters, whichever comes first.

See also.
  fputs, fgetc, gets, puts


© The C++ Resources Network, 2000