search in text file

Thread Solved
Reply

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

search in text file

 
0
  #1
Oct 9th, 2007
Hi am trying to make a simple application that write and read from and to a text file the problem is that i want to be able to aerch in the text file for an specific line.

the text file is a list of words with a meaning each word, the application ask for what word to search and i want to cout<< the specific line containing that word..

could someone can help me ??
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,149
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: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: search in text file

 
0
  #2
Oct 9th, 2007
yes -- read each line sequentiall from star to finish until you get the line you want. use fstream's getline() to read an entire line then check only the first word to see if the line is the one you want.

Don't attempt to code all of that at one time. First get your program to open the file for reading. After that is code and compiles without error, add a loop using getline() to read each line. Get that working then code the next part.
Last edited by Ancient Dragon; Oct 9th, 2007 at 9:49 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
  #3
Oct 10th, 2007
ok this is what i got

  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. string line;
  10. char word;
  11.  
  12. fstream dict("c://example.txt",ios::in);
  13.  
  14. if (!dict.is_open()){
  15. cout << "Unable to open file";
  16. exit(1);
  17. }
  18. cout<<"Enter word to search \n";
  19. cin>>word;
  20. while(getline(dict,line))
  21. cout<<line<<"\n";
  22.  
  23.  
  24. return 0;
  25. }


this is the part i know, now what ?


thanks in advanced !!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,149
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: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: search in text file

 
0
  #4
Oct 10th, 2007
expand the while loop to extract the first word (hint: use string's find() method to locate the first space), then compare that with the string that you entered from the command line. If they are the same then you can stop that loop.
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
  #5
Oct 10th, 2007
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. string line;
  10. char* word="";
  11. size_t i;
  12. fstream dict("c://example.txt",ios::in);
  13.  
  14. if (!dict.is_open()){
  15. cout << "Unable to open file \n";
  16. exit(1);
  17. }
  18. cout<<"Enter word to search \n";
  19. cin>>word;
  20.  
  21. while(getline(dict,line))
  22. i = line.find(word);
  23.  
  24. if (line.compare(word) != 0){
  25. cout << "result " <<line << endl;
  26.  
  27. }else
  28.  
  29. cout<<"Not in list"<<"\n";
  30.  
  31.  
  32. return 0;
  33. }

I have the idea of what you are saying but dont know how to put it in code
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,149
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: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: search in text file

 
0
  #6
Oct 10th, 2007
A little off. First you need to put braces around that entire while statement. Next, the find mentiond returns string::npos if the word is not found. You need to add a check to see if the find method worked or not.
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
  #7
Oct 10th, 2007
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. string line;
  10. char* word="";
  11. size_t i;
  12. fstream dict("c://example.txt",ios::in);
  13.  
  14. if (!dict.is_open()){
  15. cout << "Unable to open file \n";
  16. exit(1);
  17. }
  18. cout<<"Enter word to search \n";
  19. cin>>word;
  20. {
  21. while(getline(dict,line)) //read line by line
  22. i = line.find(word); //try to find the var word in the string line an assign it to i
  23.  
  24. if (i!=string::npos) // dont exactly know what it does
  25. cout<<i<<" not found";
  26. else
  27. line.compare(word) != 0; //compare the string line to the var word
  28. cout << "result " <<line << endl;
  29. exit (1);
  30.  
  31. }
  32.  
  33. return 0;
  34. }


I know youre trying to make me understand but am a little lost here really !!
i put comments around the lines to see if i understand what each does correctly , please correct me if am wrong..

thanks a lot
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: search in text file

 
0
  #8
Oct 10th, 2007
Why not try a trivial example first without reading a text file. When you are confident with how it works try it with a text file.
Questions (hints)
Are you getting your logic muddled up? Do you even need the line.compare(word)?
http://www.cplusplus.com/reference/s...ring/find.html
Last edited by iamthwee; Oct 10th, 2007 at 2:18 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,149
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: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: search in text file

 
0
  #9
Oct 10th, 2007
Originally Posted by reaven View Post
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. string line;
  10. char* word="";
  11. size_t i;
  12. fstream dict("c://example.txt",ios::in);
  13.  
  14. if (!dict.is_open()){
  15. cout << "Unable to open file \n";
  16. exit(1);
  17. }
  18. cout<<"Enter word to search \n";
  19. cin>>word;
  20. {
  21. while(getline(dict,line)) //read line by line
  22. i = line.find(word); //try to find the var word in the string line an assign it to i
  23.  
  24. if (i!=string::npos) // dont exactly know what it does
  25. cout<<i<<" not found";
  26. else
  27. line.compare(word) != 0; //compare the string line to the var word
  28. cout << "result " <<line << endl;
  29. exit (1);
  30.  
  31. }
  32.  
  33. return 0;
  34. }


I know youre trying to make me understand but am a little lost here really !!
i put comments around the lines to see if i understand what each does correctly , please correct me if am wrong..

thanks a lot
you put the braces in the wrong place. This is how it should look:
  1. while(getline(dict,line)) //read line by line
  2. { // start of while
  3.  
  4. // your code goes here
  5.  
  6. } // end of while
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
  #10
Oct 10th, 2007
I put it there but still dont work !!

it is my code right or wrong?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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