C standard library function fgets with example


Advertisements

Share

Description


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.

fgets header file


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.

Illustration examples

Listing1.c: Program to illustrate the usage of fgets library function.

#include <stdio.h>
#include <string.h>
void getName(char *name);
void main() {
  char myName[20];
  getName(myName);
  printf("Your name:%s\n",myName);
}
void getName(char *name) {
  printf("Enter your name:");
  fgets(name,sizeof(name),stdin);
}

Listing1.c program output.

C:\MinGW\bin>gcc C:\programs\Listing1.c
C:\MinGW\bin>a.exe
Enter your name:ram
Your name:ram
C:\MinGW\bin>a.exe
Enter your name:ramkumar
Your name:ramku
C:\MinGW\bin>        
C:\MinGW\bin>a.exe
Enter your name:
Your name:
C:\MinGW\bin>        


Listing1.c program analysis
    CASE I  In Listing1.c we used fgets to collect the input characters from standard input stdin. As noticed in the output when we entered input as ram and pressed newline(\n) character by pressing ENTER key, it stops reading from the standard input. Hence it printed output as ram.
    CASE II  As noticed in the output when we entered input as ramkumar and pressed newline(\n) character by pressing ENTER key, it stops reading from the standard input after reaching the 5th character. It reserved one character for the null character to end the string. Hence it printed output as ramku.
    CASE III  Let's run the program again and press ENTER without giving any input. Program returns pointer to a NULL string(\0) in this case and hence it prints as blank.

Listing2.c: Program using fgets library function with file as input.

#include <stdio.h>
#include <string.h>
#define MAX_COUNT 6
void getName(char *name);
void main() {
  char myName[6];
  FILE *fptr;
  fptr = fopen("C:/programs/f.txt", "r");
  if(fptr == NULL) {
    perror("Could not open file\n");
  } else {
    if(fgets(myName,MAX_COUNT,fptr) == NULL) {
      /* Empty file f.txt */
      printf("my name:%s\n",myName);
    } else {
      printf("my name:%s\n",myName);
    }
  }
  fclose(fptr);
}
Listing2.c program output.

C:\MinGW\bin>gcc C:\programs\Listing2.c
C:\MinGW\bin>a.exe
my name::ram
Your name:ram
C:\MinGW\bin>a.exe
my name::ramku
C:\MinGW\bin>a.exe
my name:
C:\MinGW\bin>        


Listing2.c program analysis
    CASE I  In Listing2.c we created a file f.txt in the same folder as Listing2.c program. We used fgets to collect the input characters from file f.txt. We wrote ram as the content of f.txt file. As noticed in the output, the program prints ram as output as it got an EOF character in the file after reading "ram" and stops reading from the file afterwards.
    CASE II  Lets write ramkumar in the file f.txt and run the program again. As noticed in the output program stops reading from the file after reaching the 5th character. It reserved one character for the null character to end the string. As maximum characters to be read is defined as 6, program prints output in this case as ramku.
    CASE III  Let's keep f.txt as empty and run the program again. As noticed in the output, program returns a NULL pointer. Hence it prints output as blank.


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 .
Share

 Articles

 C Programming

 Linux Software

 Search Code Snippets

 Popular C search examples

 Popular C search MCQs

Glass Elite Apple iPhone 11 Pro (Case Friendly)

 Test your skills:online tests

 C Programming

 Python Programming

 Linux Software

 Quantitative Aptitude

 Embedded System Software