Reads a character string from a stream.
#include <stdio.h> char *fgets(char *s, int n, FILE *stream);
s
A pointer to an array that will contain the string.
n
The maximum number of characters to read.
stream
A pointer to a file stream.
The fgets() function reads characters sequentially from stream beginning at the current file position, and assembles them into s as a character array. The function stops reading characters when n - 1 characters have been read. The fgets() function finishes reading prematurely if it reaches a newline ( '\n' ) character or the end-of-file.
If the file is opened in update mode (+) a file cannot be read from and then written to without repositioning the file using one of the file positioning functions ( fseek(), fsetpos(), or rewind()) unless the last read or write reached the end-of-file. Unlike the gets() function, fgets() appends the newline character ( '\n') to s. It also null terminates the characters written into the character array.
This facility may have limited capability on configurations of the EWL that run on platforms that do not have console input/output or a file system.
fgets() returns a pointer to s if it is successful. If it reaches the end-of-file before reading any characters, s is untouched and fgets() returns a null pointer ( NULL). If an error occurs fgets() returns a null pointer and the contents of s may be corrupted.