Find word in file!!

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

Join Date: Dec 2006
Posts: 43
Reputation: itchap is an unknown quantity at this point 
Solved Threads: 0
itchap itchap is offline Offline
Light Poster

Find word in file!!

 
0
  #1
Dec 8th, 2006
Hi everyone,

Im trying to solve this question:

write a C++ program to find a given word in a file. It should display all the line numbers where the word occurs.

this is the code that i did so far, the problem is it wont display all the right line number, it only displays "1" I hope that you guys can help me fix it up.

  1.  
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <fstream>
  5. using namespace std;
  6. int main()
  7. {
  8. string s1;
  9. ifstream in ("file.txt");
  10. int lines = 0;
  11. cout << "Enter your search string: " <<endl;
  12. cin >> s1;
  13. in.seekg(0,ios::beg);
  14. lines ++;
  15. cout<<"The string occured at lines: " << lines<<endl;
  16. in.close();
  17. getch();
  18. return 0;
  19. }

Thanks in advance,
itchap
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 3
Reputation: mcklein_tunde is an unknown quantity at this point 
Solved Threads: 0
mcklein_tunde mcklein_tunde is offline Offline
Newbie Poster

Re: Find word in file!!

 
0
  #2
Dec 8th, 2006
i am new to eriting c codes and in one of the programming i am currently doing, i came across something like #ifdef_cplusplus. i don't know what it means. help
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 43
Reputation: itchap is an unknown quantity at this point 
Solved Threads: 0
itchap itchap is offline Offline
Light Poster

Re: Find word in file!!

 
0
  #3
Dec 8th, 2006
Originally Posted by mcklein_tunde View Post
i am new to eriting c codes and in one of the programming i am currently doing, i came across something like #ifdef_cplusplus. i don't know what it means. help
You should ask this question in a new thread, so others dont get confused on which of us to help.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,847
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 299
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Roasting Maven

Re: Find word in file!!

 
0
  #4
Dec 8th, 2006
Originally Posted by itchap
in.seekg(0,ios::beg);
lines ++;
You should put this in a loop, now it will find 1 occurence and continue with the rest of your code.
so something like:

  1.  
  2. While (not the end of the file)
  3. {
  4. look for searchstring
  5. lines++;
  6. if string is found
  7. {
  8. cout << "found at line" << lines << endl
  9. }
  10. }

regards Niek
Last edited by niek_e; Dec 8th, 2006 at 8:18 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 43
Reputation: itchap is an unknown quantity at this point 
Solved Threads: 0
itchap itchap is offline Offline
Light Poster

Re: Find word in file!!

 
0
  #5
Dec 8th, 2006
Originally Posted by niek_e View Post
You should put this in a loop, now it will find 1 occurence and continue with the rest of your code.
so something like:

  1.  
  2. While (not the end of the file)
  3. {
  4. look for searchstring
  5. lines++;
  6. if string is found
  7. {
  8. cout << "found at line" << lines << endl
  9. }
  10. }

regards Niek

Thanks Niek!!!!!!!!!!!!!!!!!!!!
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC