C programming stdio.h function - char *fgets(char *str, int n, FILE *stream)

www.igi‮oc.aeditf‬m

The fgets() function is used to read a line of text from a file. It reads characters from the stream and stores them in the character array str until either a newline character '\n' is read, or n-1 characters have been read, or the end-of-file is reached, whichever comes first. The resulting string str is always null-terminated.

The function has the following prototype:

char *fgets(char *str, int n, FILE *stream);

where:

  • str is the pointer to an array of chars where the characters read are stored
  • n is the maximum number of characters to be read
  • stream is a pointer to a FILE object that identifies the stream to be read

The function returns str on success, and NULL on failure. The failure could be due to an error or the end-of-file being reached.