Signup/Sign In
LAST UPDATED: MARCH 24, 2022

C++ Program For Count A Character In File Using File

In this tutorial, we will be learning how to count a character in a file using the file.

C++ Program For Counting a Character in File Using File

Before moving to the implementation part, let's first understand the working of the algorithm:

First, we have to create a file with the extension (like .txt) after that by using the program we have to print the file on the console screen.

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
        ifstream  fin("read.txt");
        char ch;
        int i, c=0, sp=0;
        while(fin)
        {
                fin.get(ch);
                i=ch;
                if((i > 63 && i < 91) || (i > 96 && i < 123))
                        c++;
                else
                        if(ch== ' ')
                                sp++;
        }
        cout<<"\n No. of Characters in a File : "<<c;
        cout<<"\n Space between the Words     : "<<sp;
        return 0;
}


read.text
Hello
Welcome to Studytonight

No. of Characters in a File : 26

Space between the Words :2

Conclusion

Here, in this tutorial, we have implemented counting the occurrence of any character in a file using files.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.