counting a particular word in .txt file

counting a particular word in .txt file using C++

Page 1 of 1

4 Replies - 2241 Views - Last Post: 22 September 2009 - 04:53 AM Rate Topic: -----

#1 neodescorpio_143  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-September 09

counting a particular word in .txt file

Posted 16 September 2009 - 11:06 AM

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
 ofstream x;
 
 x.open("k.rtf");
 if("k.is_open()")
 {
                x <<"Write this to file and count the word while in it.\n";
                x <<"while while while while while while while while while.\n";
                x.close();
                cout<<"Text has been writen to k.txt";
 }
 else
 cout<<"Unable to open the file.";
 

 
getchar();     
 }


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 16 September 2009 - 11:31 AM


Is This A Good Question/Topic? 0
  • +

Replies To: counting a particular word in .txt file

#2 hawkysu  Icon User is offline

  • I am a spoon
  • member icon

Reputation: 5
  • View blog
  • Posts: 1,432
  • Joined: 20-February 09

Re: counting a particular word in .txt file

Posted 16 September 2009 - 11:16 AM

Please: :code:

and please tell us the exact problem. Then we can help.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: counting a particular word in .txt file

Posted 16 September 2009 - 11:32 AM

if("k.is_open()")

Why the quotes?
Was This Post Helpful? 0
  • +
  • -

#4 carltech  Icon User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • Posts: 997
  • Joined: 19-October 07

Re: counting a particular word in .txt file

Posted 16 September 2009 - 11:59 AM

yeah drop the quotes in the if

also if you wanna count a word then you can use strcmp() to check if the word is what you are looking for then if it is add to an counter.
Was This Post Helpful? 0
  • +
  • -

#5 carle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 21-September 09

Re: counting a particular word in .txt file

Posted 22 September 2009 - 04:53 AM

Why add quotes with k.is_open()? Further more, what's really your problem? Counting WHICH particular word in .txt file?


----------------------------------------------------------
carle
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1