944,008 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 10967
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 9th, 2007
0

search in text file

Expand Post »
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 ??
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
reaven is offline Offline
52 posts
since Oct 2007
Oct 9th, 2007
0

Re: search in text file

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Oct 10th, 2007
0

Re: search in text file

ok this is what i got

C++ Syntax (Toggle Plain Text)
  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 !!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
reaven is offline Offline
52 posts
since Oct 2007
Oct 10th, 2007
0

Re: search in text file

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Oct 10th, 2007
0

Re: search in text file

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
reaven is offline Offline
52 posts
since Oct 2007
Oct 10th, 2007
0

Re: search in text file

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Oct 10th, 2007
0

Re: search in text file

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
reaven is offline Offline
52 posts
since Oct 2007
Oct 10th, 2007
0

Re: search in text file

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 10th, 2007
0

Re: search in text file

Click to Expand / Collapse  Quote originally posted by reaven ...
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. while(getline(dict,line)) //read line by line
  2. { // start of while
  3.  
  4. // your code goes here
  5.  
  6. } // end of while
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Oct 10th, 2007
0

Re: search in text file

I put it there but still dont work !!

it is my code right or wrong?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
reaven is offline Offline
52 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Questions re: 2D Array w/bubble sort
Next Thread in C++ Forum Timeline: sin , cos , tan , cot , exp , pow , fabs & fibonachi simulation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC