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.
Ex:

ls cd
date
ls
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')
            num++;
    }
    is.close();
    cout<<"Number of lines in file is "<<num<<endl;
    return 0;
}

Help would be appreciated.
Thanks

Recommended Answers

All 4 Replies

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;

07

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

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

10

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 }

20

21 string line;

22 while(getline(input, line))

23 {

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

25 line_count[line]++;

26 }

27

28 map<string, int>::const_iterator

29 iter = line_count.begin(),

30 end_it = line_count.end();

31

32 while(iter != end_it)

33 {

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

35 iter++;

36 }

37 cout << endl;

38

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.

Thanks

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!

Oh Ok .. will try .. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.