fgets function(Read String from File) in C Language – example and detail

In the C Programming Language, the fgets function reads characters from the circulate pointed to through stream.

The fgets function will end reading when n-1 characters are read, the first new-line character is encountered in s, or at the end-of-file, whichever comes first. Then the fgets function will append a null personality to the string.


The syntax for the fgets feature in the C Language is:

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

Parameters or Arguments

s The array where the characters that are study will be stored. n The dimension of s. stream The move to read.


The fgets function returns s. The fgets function will return a null pointer if an error occurs while trying to study the stream or the give up of the circulate is encountered earlier than any characters are stored.

Required Header

In the C Language, the required header for the fgets function is:

#include <stdio.h>

Applies To

In the C Language, the fgets feature can be used in the following versions:

ANSI/ISO 9899-1990

Similar Functions

Other C functions that are similar to the fgets function:

gets function <stdio.h>

See Also

Other C features that are noteworthy when dealing with the fgets function:

fputs function <stdio.h>

puts function <stdio.h>