search in text file

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2005
Posts: 15,678
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1503
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: search in text file

 
0
  #11
Oct 10th, 2007
re-post your most recent code. What you posted earlier was not right, as I mentioned before. Pay close attention to the placement of the braces. compilers do not accept sloppy coding.
Last edited by Ancient Dragon; Oct 10th, 2007 at 11:30 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 52
Reputation: reaven is an unknown quantity at this point 
Solved Threads: 0
reaven reaven is offline Offline
Junior Poster in Training

Re: search in text file

 
0
  #12
Oct 11th, 2007
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <ctype.h>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.  
  11. string line;
  12. char word[65];
  13. size_t found=' ';
  14.  
  15. fstream dict("c://dict.txt",ios::in);
  16.  
  17. if (!dict.is_open()){
  18. cout << "Unable to open file \n";
  19. exit(1);
  20. }
  21. cout<<"Welcome \n"
  22. <<"Cois 270 \n"
  23. <<"\n"
  24. <<"\n"<<endl;
  25.  
  26. cout<<"Enter a word to search: ";
  27. cin>>word;
  28.  
  29. while(getline(dict,line)){
  30. if ( (found = line.find (word, 0)) != string::npos )
  31.  
  32. cout<<"\n"<<line<<endl;
  33. }
  34.  
  35. if ( (found != line.find (word, 0)) == string::npos )
  36. cout<<"'"<<word<<"'"<<" is not found \n";
  37.  
  38. dict.close();
  39. return 0;
  40.  
  41. }


somehow or someway i get it to work, dont know if is the right way to do it but it work...

I limit how much is display in the result by
  1. char word[65]="";
if i let the char word; alone it display the entire text file starting from the word i was searching.

now am trying to convert the char word to lowercase in case someone type a word in upper
I try to use cast and tolower() but cast dont work and tolower() can convert int to char[65]...


Thanks a lot " Ancient Dragon " this is the best way to learn !!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,678
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1503
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: search in text file

 
0
  #13
Oct 11th, 2007
that while loop is close to being ok, but not quite. If the word is found you need to jump out of that loop if you don't then the if statement that is after the loop will not work correctly.

That loop is also still incorrect because the return value of find could be somewhere in the middle of the string, which is not what you want. For example, lets say the word you want to find is "Mary" and the sentense is "Hello my name is Mary.". In this example the find method will return a value of 17 because "Mary" starts in position 17 of the string. But you want a string with the word "Mary" at the beginning of the string and ignore other places. This means that if statement has to be changed to check if found is 0, not npos.
  1. while(getline(dict,line)){
  2. if ( (found = line.find (word, 0)) == 0 )
  3. {
  4. cout<<"\n"<<line<<endl;
  5. break;
  6. }
  7. }

to convert to lower-case you need to call tolower() in a loop and call it for every letter in the word. There is another way, assuming word is std::string object and not a character array.
  1. translate(word.begin(), word.end(), word.begin(), tolower);

You will also have to do the same thing with each line read from the file, just to insure that they are all the same case.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 52
Reputation: reaven is an unknown quantity at this point 
Solved Threads: 0
reaven reaven is offline Offline
Junior Poster in Training

Re: search in text file

 
0
  #14
Oct 11th, 2007
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <ctype.h>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.  
  11. string line;
  12. char word[65]="";
  13. char ans=' ';
  14. size_t found=' ';
  15.  
  16. fstream dict("c://dict.txt",ios::in);
  17.  
  18. if (!dict.is_open()){
  19. cout << "Unable to open file \n";
  20. exit(1);
  21. }
  22. cout<<"Welcomey\n"
  23. <<"Cois 270\n"
  24. <<"\n"
  25. <<"\n"<<endl;
  26. while(toupper(ans)!='X'){
  27. cout<<"Enter a word to search: ";
  28. cin>>word;
  29.  
  30. while(getline(dict,line)){
  31. if ( (found = line.find (word, 0)) != string::npos )
  32.  
  33. cout<<"\n"<<line<<endl;
  34. }
  35.  
  36. if ( (found != line.find (word, 0)) == string::npos ){
  37. cout<<"'"<<word<<"'"<<" is not found \n";
  38. }
  39. cout<<"Press 'X' to exit or any other key to continue searching ";
  40. cin>>ans;
  41. }
  42. dict.close();
  43. return 0;
  44.  
  45. }

for now i add a loop to see if you want to search more words or not, but now the if statement that check if the word is not found is not validating and maybe is the braces

thanks
Last edited by reaven; Oct 11th, 2007 at 12:35 am. Reason: double post
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,678
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1503
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: search in text file

 
0
  #15
Oct 11th, 2007
you obviously ignored my previous post so I'll ignore yours.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 52
Reputation: reaven is an unknown quantity at this point 
Solved Threads: 0
reaven reaven is offline Offline
Junior Poster in Training

Re: search in text file

 
0
  #16
Oct 11th, 2007
i change to ==0 but when i type a word that i know is in the text file at the beginning of the line it validate for !=0
BTW i check different words all validate by !=0 and always display the same line from the text file
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 52
Reputation: reaven is an unknown quantity at this point 
Solved Threads: 0
reaven reaven is offline Offline
Junior Poster in Training

Re: search in text file

 
0
  #17
Oct 11th, 2007
you obviously ignored my previous post so I'll ignore yours.
what ???
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 52
Reputation: reaven is an unknown quantity at this point 
Solved Threads: 0
reaven reaven is offline Offline
Junior Poster in Training

Re: search in text file

 
0
  #18
Oct 11th, 2007
anyway the line that display is the first line in the text file...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,678
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1503
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: search in text file

 
0
  #19
Oct 11th, 2007
well, when you get around to posting something that includes the fix that I last mentioned I might get around to answering you. In any event -- to late for me tonight, going to sleep. Good night.

AD
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 52
Reputation: reaven is an unknown quantity at this point 
Solved Threads: 0
reaven reaven is offline Offline
Junior Poster in Training

Re: search in text file

 
0
  #20
Oct 11th, 2007
I already did what you said
i change to ==0 but when i type a word that i know is in the text file at the beginning of the line it validate for !=0
BTW i check different words all validate by !=0 and always display the same line from the text file(the line is the first one from the text file)

i post the other code because i didn't receive an answer so i move on to other thing when i was typing that you were typing your post ...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 7240 | Replies: 35
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC