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

Re: c++ find word from text file

 
0
  #11
Dec 11th, 2008
thank you
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,909
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: 274
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: c++ find word from text file

 
0
  #12
Dec 11th, 2008
Originally Posted by ddanbe
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.
I can understand that is is all new to you, and that it is difficult to grasp it all, but why use char array when there is a string class? Why do you refuse to read or look up documentation?
You do #include <string> so why don't you use it?
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
  #13
Dec 11th, 2008
i doesn't return to loop ,, how can i do??
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

 
-1
  #14
Dec 11th, 2008
i got a result:::

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

int main()
{
const int i=20;
char word[i];
int Counter;
cout<<"Enter word to search \n";
cin.get(word,i);


char fileName[80];
string buffer; // for user input
cout << "Enter file name: ";
cin >> fileName;


ifstream fin(fileName);
if(!fin.is_open())
{
cout << "Unable to open file";
system("pause");
exit(1);
}

Counter=0;
while(fin!='\0')
{
getline(fin,buffer);

size_t a = buffer.find(word,0);
if (a != string::npos)
{
cout<<buffer<<endl;
Counter++;
size_t b = a + 1;
a = buffer.find (word, b);
}
}
if(Counter>1)

cout << "Total "<<Counter<<" tLines\n";
else
cout << "Total "<<Counter<<" Line\n";
system("pause");
return 0;
}
____________________________________________

So,how can i link this program to dos??
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: c++ find word from text file

 
0
  #15
Dec 12th, 2008
Originally Posted by ojung View Post
cout<<"Enter word to search \n";
cin>>word[i];
that should be
  1. cin >> word;
, because word[i] is a char, and word is a char array
while(dict.getline(text,300))

{
if(word[j]!=text[j])
{
Likewise this should be
  1. ...
  2. if(string(text).find(word) != string::npos){
  3. ...
, otherwise you are comparing the first character only, and not the whole word
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