Count similar lines in a file

Count similar lines in a file

Page 1 of 1

13 Replies - 1096 Views - Last Post: 15 October 2010 - 01:30 PM Rate Topic: ***** 1 Votes

#1 chintan_1671  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 22-December 08

Count similar lines in a file

Posted 15 October 2010 - 07:04 AM

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 really appreciated.
Thanks

This post has been edited by JackOfAllTrades: 15 October 2010 - 07:20 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Count similar lines in a file

#2 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: Count similar lines in a file

Posted 15 October 2010 - 07:08 AM

Please post code in code tags :code:
Was This Post Helpful? 0
  • +
  • -

#3 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Count similar lines in a file

Posted 15 October 2010 - 07:10 AM

Quote

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

Don't you mean how many words are in a line?

What errors are you having?

This post has been edited by seeP+: 15 October 2010 - 07:11 AM

Was This Post Helpful? 0
  • +
  • -

#4 chintan_1671  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 22-December 08

Re: Count similar lines in a file

Posted 15 October 2010 - 07:21 AM

View PostseeP+, on 15 October 2010 - 06:10 AM, said:

Quote

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

Don't you mean how many words are in a line?

What errors are you having?


No not words. Its confusing. My bad.
If you see .. its no of similar lines. I want to count number of similar lines.
I jst have the code to count lines.
How to scan a line and save at a place and check every time when next line is read for similarity?
What algorithm should i use ?

Please help.
Thanks
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,605
  • Joined: 23-August 08

Re: Count similar lines in a file

Posted 15 October 2010 - 07:25 AM

std::map<std::string, unsigned int> would be my first attempt.

EDIT: Yeah, 26 total lines of code, with includes.

This post has been edited by JackOfAllTrades: 15 October 2010 - 07:35 AM

Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Count similar lines in a file

Posted 15 October 2010 - 07:25 AM

View PostseeP+, on 15 October 2010 - 11:10 PM, said:

Don't you mean how many words are in a line?

@seeP+
I'm confident chintan_1671 is supposed to count the number of instances of an identical line.
The example was not well chosen.

@chintan_1671
The code you present doesn't even try to do what you need to do.
Have you made a serious effort effort yet?
When you have serious effort to share please use code tags like this :code:
Was This Post Helpful? 0
  • +
  • -

#7 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Count similar lines in a file

Posted 15 October 2010 - 07:25 AM

Read the line into a string then use a string function to compare it to.
String functions
Was This Post Helpful? 0
  • +
  • -

#8 chintan_1671  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 22-December 08

Re: Count similar lines in a file

Posted 15 October 2010 - 10:25 AM

View PostJackOfAllTrades, on 15 October 2010 - 06:25 AM, said:

std::map<std::string, unsigned int> would be my first attempt.

EDIT: Yeah, 26 total lines of code, with includes.


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.


#include <iostream> 
#include <string> 
#include <map> 
#include<fstream>
#include<algorithm>
using namespace std; 

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

int main() 
{ 
	map<string, int> line_count; 
	ifstream input("1818.txt");
	if(input.fail())
	{
		cerr<<"\nThe file could not be opened.";
		return -1;
	}
	
	string line;
	while(getline(input, line))
	{ 
		transform(line.begin(),line.end(),line.begin(),::tolower);
		line_count[line]++; 
	}

	map<string, int>::const_iterator 
	iter = line_count.begin(), 
	end_it = line_count.end();
	
	while(iter != end_it) 
	{
		cout << iter->first << " # " << iter->second <<endl; 
		iter++;
	}
	cout << endl;

	//process_file(input, line_count);
	//display_word_count(line_count);
}



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
Was This Post Helpful? 1
  • +
  • -

#9 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Count similar lines in a file

Posted 15 October 2010 - 10:32 AM

are values in maps initialized to zero? i think there just allocated and what ever value is there is the value it starts as. so what you need to do is check to see if the map contains the line or not, if not set it to 1, if so increment it.
Was This Post Helpful? 0
  • +
  • -

#10 chintan_1671  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 22-December 08

Re: Count similar lines in a file

Posted 15 October 2010 - 10:39 AM

View Postishkabible, on 15 October 2010 - 09:32 AM, said:

are values in maps initialized to zero? i think there just allocated and what ever value is there is the value it starts as. so what you need to do is check to see if the map contains the line or not, if not set it to 1, if so increment it.


The program is displaying proper line with count. There is no problem in that.
The output is displayed sorted.

I dont know that your reply was for that or not.
If yes then please elaborate.

Thanks
Was This Post Helpful? 0
  • +
  • -

#11 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Count similar lines in a file

Posted 15 October 2010 - 12:48 PM

Quote

I dont want that (sorted output).
What do you want?
Was This Post Helpful? 0
  • +
  • -

#12 chintan_1671  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 22-December 08

Re: Count similar lines in a file

Posted 15 October 2010 - 01:08 PM

View PostOler1s, on 15 October 2010 - 11:48 AM, said:

Quote

I dont want that (sorted output).
What do you want?


if the current input file is:

This is a
Is this b
This is a
Is this b

I am getting output as

Is this b # 2
This is a # 2

It is sorting as per alphabetical order.
I dont want it to sort.
I want o/p in original order:

This is a # 2
Is this b # 2
Was This Post Helpful? 0
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,605
  • Joined: 23-August 08

Re: Count similar lines in a file

Posted 15 October 2010 - 01:28 PM

If you need unsorted results, then I guess std::map is not the appropriate container as it automatically sorts by key, as you've discovered. Or, instead of using an iterator for printing you could rewind the file stream and read line by line, accessing the map via the key for that line and printing the count.
Was This Post Helpful? 0
  • +
  • -

#14 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Count similar lines in a file

Posted 15 October 2010 - 01:30 PM

Then you need to come up with a modification that allows you to store the order in which you encounter each distinct sentence. A std::map orders based on the keys.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1