c++ find word from text file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 10
Reputation: ojung has a little shameless behaviour in the past 
Solved Threads: 0
ojung ojung is offline Offline
Newbie Poster

c++ find word from text file

 
0
  #1
Dec 10th, 2008
can anyone help me!? i have to create program to search word from text file, but i don't know how can i match word from input to text.. Can anybody help!!!??
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,923
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: c++ find word from text file

 
0
  #2
Dec 10th, 2008
First you have to extract a word from your text file.
Second compare it with the word you want to search.
true?-->found!
false?-->extract the next word from the file and compare again etc.
until the end of file is reached.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 10
Reputation: ojung has a little shameless behaviour in the past 
Solved Threads: 0
ojung ojung is offline Offline
Newbie Poster

Re: c++ find word from text file

 
0
  #3
Dec 10th, 2008
i can open text file, but i can't compare word ..Could you tell me which function can i use for compare it,,Please... thank you
fstream dict("test.txt",ios::in);

if (!dict.is_open())
{
cout << "Unable to open file";
system("pause");
exit(1);
}
while (! myfile1.eof() )
{
getline (myfile1,line);
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,923
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: c++ find word from text file

 
0
  #4
Dec 10th, 2008
C++ has a string class which contains a compare method. You certainly have some documentation where you can get info about those. If you can find it you can even do better.
Last edited by ddanbe; Dec 10th, 2008 at 6:59 pm.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,151
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 145
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster

Re: c++ find word from text file

 
0
  #5
Dec 10th, 2008
You could use tags. For example:
//Proper libaries//
...

  1. ifstream infile("text.txt")
  2. ofstream outfile("search.txt")
  3. ...
  4. ...
  5. ...
  6. bool tag=flase;
  7. char Word[500];
  8. //Searches word by word;
  9. do
  10. {
  11. cin.getline(Word,500)
  12.  
  13. //check if word matches;
  14. if(word[500]=="your word"
  15. {
  16. tag=true;
  17. break;
  18. }
  19. } while(!infile.eof());
//So basically search the whole file and keep tab on which word you have imported and if your word matches then, break;
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 10
Reputation: ojung has a little shameless behaviour in the past 
Solved Threads: 0
ojung ojung is offline Offline
Newbie Poster

Re: c++ find word from text file

 
0
  #6
Dec 10th, 2008
thank you so much for advice,,, i'll try
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1
Reputation: swyncoop is an unknown quantity at this point 
Solved Threads: 0
swyncoop swyncoop is offline Offline
Newbie Poster

Re: c++ find word from text file

 
0
  #7
Dec 11th, 2008
Originally Posted by ojung View Post
can anyone help me!? i have to create program to search word from text file, but i don't know how can i match word from input to text.. Can anybody help!!!??
Not much there to go on but it appears you have no indices therefore can use a simple character compare by stepping through the text. The problem is that this type of compare can be very time consumming.

Alternately, you might be able to use the built in library routines for text string handling and simply look for the target text within the text to be searched. Might be the simplest and fastest way to go.

You should find examples of either method in the complier's docs or any basic programming manual.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: c++ find word from text file

 
1
  #8
Dec 11th, 2008
My advice, do not use eof() it's counter productive. Instead do this.

  1. while(getline(file, someString)){
  2. //yourcode
  3. }
Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 10
Reputation: ojung has a little shameless behaviour in the past 
Solved Threads: 0
ojung ojung is offline Offline
Newbie Poster

Re: c++ find word from text file

 
0
  #9
Dec 11th, 2008
i don't know why it doesn't match,,

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

int main()
{ int i,j=0;

char word[30];
char text[300];
cout<<"Enter word to search \n";
cin>>word[i];
fstream dict("test.txt",ios::in);

if (!dict.is_open())
{
cout << "Unable to open file";
system("pause");
exit(1);
}
loop:
while(dict.getline(text,300))

{
if(word[j]!=text[j])
{


goto loop;
j++;
}
else;
cout<<text<<endl;}
cout<<endl;
system("pause");
return 0;
}
..................................

i'm really new,,,so confuse..
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: c++ find word from text file

 
0
  #10
Dec 11th, 2008
Please use code tags.
Secondly, avoid goto statements, there are much better ways of achieving the same results. http://www.cprogramming.com/tutorial/goto.html

Secondly there is no word[30] to word[299], they don't exsist so thats a very bad move to make. Also if the character matches or not you still go back and read a new line from your inputfile.

There are probably many more things i could comment on but im not going to for now at least.

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC