C standard library function fgets with example
Advertisements
The C library function char *fgets(char *str, int n, FILE *fptr) reads limited number of characters from a specified stream as defined by fptr and stores the characters into a string pointed to by str. The result stored in str is appended with a null character. It stops reading from the defined stream once n-1 characters are found or an EOF character or newline character(\n) is found whichever comes first. fgets stands for file get string.
The function fgets is included in the C standard library header file
stdio.h.
fgets function declaration, arguments
The function fgets is declared as the following.
char *fgets(char *str, int n, FILE *fptr);
str This is a pointer to a string where characters are stored.
n n is the maximum number of characters to be read from the stream including a null character(\0). If a newline(\n) character or EOF character is found, it stops reading from stream even though n-1 characters are not read.
fptr This is a pointer to the FILE object or standard input(stdin) object that determines the stream to be used to read the input characters.
fgets function return value
fgets function returns a pointer to the string "str" where it has stored the characters after reading from the input stream. In case there are no characters found on the input stream except EOF it returns a NULL pointer.
To Read Full Article
You need to subscribe & logged in
Subscribe
C programming exercises using fgets function.
EXERCISE 1 Write a c program that uses fgets library function to display the first line from a file.
Want to contribute or ask a new article? Write and upload your article information
here.