Table of Contents
Previous Section Next Section

fgets

#include <stdio.h>char *fgets(char *str, int num, FILE *stream);

The fgets( ) function reads up to num–1 characters from stream and stores them in the character array pointed to by str. Characters are read until either a newline or an EOF is received or until the specified limit is reached. After the characters have been read, a null is stored in the array immediately after the last character read. A newline character will be retained and will be part of the array pointed to by str.

In C99, str and stream are qualified by restrict.

If successful, fgets( ) returns str; a null pointer is returned upon failure. If a read error occurs, the contents of the array pointed to by str are indeterminate. Because a null pointer will be returned either when an error has occurred
or when the end of the file is reached, you should use feof( ) or ferror( ) to determine what has actually happened.

Related functions are fputs( ), fgetc( ), gets( ), and puts( ).


Table of Contents
Previous Section Next Section