C Programming

fgetc Function in C

File is one of the crucial topics in the C language. To understand the C language, file handling is necessary to learn. In the C language, a standard predefined function exists in the file. The name of the function is fgetc(). Let’s discuss the concept and application of the fgetc() function. How does this function work? We will explain it with the help of some important programming languages.

Syntax

The general syntax of the fgetc() function is:

int fgetc( FILE *pointer)

Here, the pointer to a FILE object is passed as an argument to access the desired file and perform a certain operation on the file.

Programming Example 1

In this programming example, we will explain the usefulness and the implementation of the fgetc() function:

#include <stdio.h>

void main()
{  
FILE *fp;   // Declaring a file pointer.
fp = fopen("file.txt", "w");        // file will be opened in write mode.
fprintf(fp, " Reading data from a file is a common feature of the file\n");
fclose(fp);

char ch;
fp = fopen("file.txt", "r");  
while((ch=fgetc(fp))!=EOF)// fgetc () function is called to read character from the file.
{  
printf("%c",ch);  // print the each character.
}  
fclose(fp);        
}

Output

File Content Screenshot

Explanation

In this programming example, we will open a file named “file.txt” in a writing mode with the help of the fopen() function and write some inputs in the file. After closing the file with the help of the fclose() function, we will open this particular file again in reading mode to take each single character from the file. For this, we will call the fgetc() function as an argument inside the parenthesis of the while loop, and this loop will be iterated until we reach the last character of the file by using EOF. EOF stands for the end of file. Then, we will print each character.

Programming Example 2

Here, we will see another important example of fgetc() function:

#include <stdio.h>

int main()
{
   FILE *fp;    // using a file pointer.
fp = fopen("myfile.txt", "r");  // file will open in reading mode.
   char chr;

   if (fp != NULL) {

    while (!feof(fp))   // expecting the end of file.
 {
        chr = fgetc(fp);    // calling the fgetc () function.

        printf("Here we are reading character from the given text file = %c \n", chr);      // reading the each character from the file.
    }
    fclose(fp);
   }
   else {
    printf("\n Unable to Create or Open the text File");
   }
   return 0;
}

Output

File Content Screenshot

Explanation

Here, we will open a file named “myfile.txt” using the fopen() function in reading mode. This file already contained some data. Now, we want to read each character from this file. For this, we will call the fgetc() function, which reads each character from this file and stores it inside a character type variable named “ch”. Since we will reach the last character from the file, we will use the feof() function to terminate the loop. Now, we will print each character.

Programming Example 3

In this programming example, we will see the last important feature of fgetc() function:

#include <stdio.h>

int main ()
{
  FILE * fp;    // a file pointer will be created
  int x;
  int i = 0;
fp= fopen("comma.txt" ,"r");        // File is opened in read mode.
  if (fp == NULL) perror ("No file found");
  else
  {
    do
        {
      x = fgetc(fp);    // calling fgetc () function.
      if (x == ',')
i++;
    }
    while (x != EOF);
fclose(fp);    
printf("This text file contains %d comma sign characters (,) in the sentence .\n",i);
  }
  return 0;
}

Output

File Content Screenshot

Explanation

Here, we will open a file named “comma.txt” using the fopen() function in reading mode. This file already contained some data. Now, we want to count a special character named comma (,) from this file. For this, we will call fgetc() function which reads each special character from this file and stores it inside an integer type variable named “x”. Since we will reach the last character from the file, we will use EOF to terminate the loop. This is how to print the total number of commas in this file.

Conclusion

This article discussed in detail the concept of the fgetc() function, we realized that this particular function helps the coder acquire each character individually from the file. For this, we can easily recognize each and every character written in the file. This function is often used in the C language in the concept of the File arena.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com