Hi i have a code which counts no of lines in a code. I want something like it reads a file and counts all lines in a code.

ls cd
ls cd
cd ls
cd ls

I want to print output like:

ls cd # 2
date # 1
ls # 1
cd ls # 2

Here is my code to count lines in a file:

#include <iostream>
#include <fstream>
using namespace std;

int main () 
    char c;
    int num=0;
    ifstream is;
    is.open ("1818.txt");
    while (is.good())
        c = is.get(); 
        if (c=='\n')
    cout<<"Number of lines in file is "<<num<<endl;
    return 0;

Help would be appreciated.

Edited by mike_2000_17: Fixed formatting

6 Years
Discussion Span
Last Post by chintan_1671

Why not save each line as a string (whichever flavor you prefer), put those into any container/array, and then count them from there?

You could easily do it with a multimap<std::string,int> for example, where int is incremented for each equivalent std::string, or set to 1 if the element did not exist.
Then iterate through the map from map.begin(), and output the string (iterator->first), followed by however you would format the count (iterator->second)


Hi i was able to develop a code.
It works fine. Thanks for pointing it out for using map<>.

But i am nt getting perfect output as i want.

view sourceprint?01 #include <iostream>

02 #include <string>

03 #include <map>

04 #include<fstream>

05 #include<algorithm>

06 using namespace std;


08 //void process_file(ifstream, map<string,int>&);

09 //void display_line_count (const map<string,int>&);


11 int main()

12 {

13 map<string, int> line_count;

14 ifstream input("1818.txt");

15 if(input.fail())

16 {

17 cerr<<"\nThe file could not be opened.";

18 return -1;

19 }


21 string line;

22 while(getline(input, line))

23 {

24 transform(line.begin(),line.end(),line.begin(),::tolower);

25 line_count[line]++;

26 }


28 map<string, int>::const_iterator

29 iter = line_count.begin(),

30 end_it = line_count.end();


32 while(iter != end_it)

33 {

34 cout << iter->first << " # " << iter->second <<endl;

35 iter++;

36 }

37 cout << endl;


39 //process_file(input, line_count);

40 //display_word_count(line_count);

41 }

The code is counting lines properly.
But while displaying output .. it is sorting the data.
I mean the line which starts with a is displayed on top with count.
I dont want that.

Can any one please help me out with this.
Also is there anyway to optimise the code or something is wrong in code.



Sorry, I mentioned multimap for inserting equivalent elements, but mostly lost my train of thought in the explanation.

Also, please use code tags.

Try storing a struct instead of an int, example:

struct line_info {
  int line, count;
  line_info() : line(-1), count(-1) { }
  line_info( int a, int b ) : line(a), count(b) { }

  bool operator<(const line_info &ln) const {
    return this->line < ln.line;

It's important to note that I overloaded the operator less than, because std::map uses less than to sort.

The issue with this approach is that it will only output each string one time, if you want to preserve the placement of each string, you'll need to add some complexity, such as including an array in the struct, and storing each line number separately.

Good luck!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.