C++ | Count Number of Lines In a File

Hello peeps! Lord Hypersonic greets you. Today I am presenting you a new c++ program, which can count the number of lines in a file. You just have to enter the full name of the file (by full name I mean file-location/file name, for example, C:/Users/ACB/Documents/test.txt ), then it will count the number of lines in the given file.


How the program works? What is the Logic behind this program?


1. First, it declares an integer variable c as a counter and initializes it with zero (Its value will be increased after reading each line of the file).
2. Then it asks the user to enter the full name of a file. It stores the file name in a string variable name file (by full name I mean file name like this: file-location/filename.extention for e.g., C:/Users/ABC/Documents/Test.txt). It will be more easy for you to understand if you know how cd command works.
3. Then it will declare the file input stream of name fin, after that it checks if the given file is available or not.
4. If the file exists, then it will read the file until the end of the file. It will copy each line of the file to string name line and increase the value of by 1 (i.e., c=c+1). Continue the loop until the file is finished. After reading of the file is finished, it will close the file input stream (i mean it will close the file).
5. After closing file, it will print the number of lines which is stored in integer (as it's value is increased by one after reading each line of the file).
6. If the program is not able to find the file then it will print file not exist.


This is how you can create a program which can count the number of lines used in a file. This program can be used to count the number of words in a dictionary file, as each word is stored in each line. For example words (alone, birds, happy.....etc) all these words are stored in separate lines like this:-
alone
birds
happy
It becomes easy to count the number of words by using the program which can calculate the number of lines. And you can now create your own program which can count the number of lines in a file.



Source Code:-

/*

Program: Count Number of line in a life
Description: This program can count the number of lines used in a file. It can be helpful when you want to know how many lines it takes to create a specific file like big software and wordlists.
Author: Lord Hypersonic
Email: lordhypersonic.522@gmail.com
website: www.lordhypersonic.blogspot.com

*/

#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>

using namespace std;

int main()
{
    int c=0; //declaring counter
    string file,line;// declaring file and line for taking input for filename and processing lines of life respectively.
    cout<<"Enter full name of file: "; getline(cin,file); //asking for file name.
    ifstream fin(file.c_str());
    if(fin)  //checking availability of file
    {
        while(getline(fin,line)) // running loop until end of file.
        {
            c++; //increasing value of counter by 1
        }
        fin.close(); //closing file
        cout<<"There are "<<c<<" number of line in "<<file<<endl; //printing number of files
    }
    else cout<<file<<" not exist"<<endl; //if file do not exist.
    _getch();
    return 0;
}

You can use any IDE.....
I USE CODE::BLOCKS

Working of the program:-


ENJOY 😄

No comments

Powered by Blogger.