find string in txt file

Reply

Join Date: May 2008
Posts: 99
Reputation: FTProtocol has a little shameless behaviour in the past 
Solved Threads: 1
FTProtocol FTProtocol is offline Offline
Junior Poster in Training

find string in txt file

 
0
  #1
Dec 2nd, 2008
ok so im trying to find a certain string in a txt file and once its found it needs to skip that line, the next line then add the following 2 lines into their own buffers.

EG:

Example ofomgwtfcantfindit kthx

Woot
Bang

asdasd
'asdasd
asdasd


so it finds the string "ofomgwtfcantfindit then it would skip that line, the blank line under it and add Woot to buffer1 and Bang to buffer2.

anyone have any ideas
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 99
Reputation: FTProtocol has a little shameless behaviour in the past 
Solved Threads: 1
FTProtocol FTProtocol is offline Offline
Junior Poster in Training

Re: find string in txt file

 
0
  #2
Dec 2nd, 2008
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. ifstream RapidShit ("C:\\Rapid.txt");
  9. string ****Nipples;
  10.  
  11. if(RapidShit.is_open())
  12. {
  13. while (! RapidShit.eof() )
  14. {
  15. getline(RapidShit, ****Nipples);
  16. if(****Nipples == "asdasdasd")
  17. {
  18. // how to skip 2 lines and get the other 2 into the buffers
  19. }
  20. }
  21. }
  22. cin.get();
  23. return 0;
  24. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,142
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: 1434
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: find string in txt file

 
1
  #3
Dec 2nd, 2008
>>string ****Nipples;

OMG what in hell are you trying to do here??? If you are trying to code an array of string's then its string Nipples[4];
But the reading part should look like this: (I hope you change the variable names before turning in the assignment because your teacher may not be that humerious.)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. ifstream RapidShit ("C:\\Rapid.txt");
  10. vector<string> Nipples; // an array of strings
  11. string line; // one line in the file
  12.  
  13. if(RapidShit.is_open())
  14. {
  15. while (getline(RapidShit, line) )
  16. {
  17. Nipples.push_back(line); // add to the array
  18. }
  19. // now you have the entire file in memory. Search each line
  20. // for the desired entry.
  21. }
  22. cin.get();
  23. return 0;
  24. }
Last edited by Ancient Dragon; Dec 2nd, 2008 at 8:37 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: May 2008
Posts: 99
Reputation: FTProtocol has a little shameless behaviour in the past 
Solved Threads: 1
FTProtocol FTProtocol is offline Offline
Junior Poster in Training

Re: find string in txt file

 
0
  #4
Dec 2nd, 2008
Lol, this isnt for an assignment. I code because i can not because i have to but this is what i have so far.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. char szUser[50], szPass[50];
  7. bool bFound = false;
  8.  
  9. int main()
  10. {
  11. ifstream RapidShit ("C:\\Rapid.txt");
  12. string ****Nipples;
  13.  
  14. if(RapidShit.is_open())
  15. {
  16. while (!bFound)
  17. {
  18. getline(RapidShit, ****Nipples);
  19. cout << ****Nipples << endl;
  20.  
  21. if(****Nipples == "WORD")
  22. {
  23. getline(RapidShit, ****Nipples);
  24. cout << ****Nipples << endl;
  25. getline(RapidShit, ****Nipples); // First buffer i need
  26. cout << ****Nipples << endl;
  27. getline(RapidShit, ****Nipples); // Second
  28. cout << ****Nipples << endl;
  29. bFound = true;
  30. }
  31. }
  32. }
  33. cin.get();
  34. return 0;
  35. }

Just wonder how i can store those First buffer line and Second buffer line in char szBuffer1[50], szBuffer2[50];

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

Re: find string in txt file

 
0
  #5
Dec 2nd, 2008
>>string ****Nipples;
Forget about doing that! It won't work. That is not four strings, but a pointer to pointer to pointer to pointer of type string. Amazingly it compiles without error, but that doesn't mean it will work, which it won't.
Last edited by Ancient Dragon; Dec 2nd, 2008 at 10:18 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: May 2008
Posts: 99
Reputation: FTProtocol has a little shameless behaviour in the past 
Solved Threads: 1
FTProtocol FTProtocol is offline Offline
Junior Poster in Training

Re: find string in txt file

 
0
  #6
Dec 2nd, 2008
oh it works

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. char szUser[50], szPass[50];
  7. bool bFound = false;
  8.  
  9. void GetUser(string szInput)
  10. {
  11. const char *szUserBuffer;
  12.  
  13. std::string szTemp = szInput;
  14. szUserBuffer = szTemp.c_str();
  15. sprintf(szUser, "%s", szUserBuffer);
  16.  
  17. for (int i = strlen(szUser) + 1; i > 0; i--)
  18. {
  19. if( szUser[i] == ':')
  20. {
  21. strcpy(szUser, &szUser[i + 2]);
  22. break;
  23. }
  24. }
  25. }
  26.  
  27. void GetPassword(string szInput)
  28. {
  29. const char *szPassBuffer;
  30.  
  31. std::string szTemp = szInput;
  32. szPassBuffer = szTemp.c_str();
  33. sprintf(szPass, "%s", szPassBuffer);
  34.  
  35. for (int t = strlen(szPass) + 1; t > 0; t--)
  36. {
  37. if( szPass[t] == ':')
  38. {
  39. strcpy(szPass, &szPass[t + 2]);
  40. break;
  41. }
  42. }
  43. }
  44.  
  45. int main()
  46. {
  47. ifstream RapidShit ("C:\\Rapid.txt");
  48. string ****Nipples;
  49.  
  50. if(RapidShit.is_open())
  51. {
  52. while (!bFound)
  53. {
  54. getline(RapidShit, ****Nipples);
  55. if(****Nipples == "https://ssl.rapidshare.com")
  56. {
  57. getline(RapidShit, ****Nipples);
  58. getline(RapidShit, ****Nipples);
  59. GetUser(****Nipples);
  60. getline(RapidShit, ****Nipples);
  61. GetPassword(****Nipples);
  62. bFound = true;
  63. }
  64. }
  65. }
  66. cin.get();
  67. return 0;
  68. }

Probably far too long but it works.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,142
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: 1434
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: find string in txt file

 
0
  #7
Dec 2nd, 2008
>>Probably far too long but it works.
Only because you were very lucky in your tests.

>>getline(RapidShit, ****Nipples);
What does that pointer Nipples point to? Nowhere -- just some random number because Nipples was never initialized.
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: May 2008
Posts: 99
Reputation: FTProtocol has a little shameless behaviour in the past 
Solved Threads: 1
FTProtocol FTProtocol is offline Offline
Junior Poster in Training

Re: find string in txt file

 
0
  #8
Dec 4th, 2008
lol im not lucky it works 100%
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 921
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: find string in txt file

 
0
  #9
Dec 4th, 2008
Originally Posted by FTProtocol View Post
lol im not lucky it works 100%
Then what's the point of this thread, anymore?
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,381
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 112
Sponsor
William Hemsworth William Hemsworth is online now Online
Nearly a Posting Virtuoso

Re: find string in txt file

 
0
  #10
Dec 4th, 2008
Originally Posted by Ancient Dragon
Amazingly it compiles without error
Amazingly, this compiles without error (for me)

So I'm not too surprised that his compiled x)
  1. char ********************************************************************************
  2. ********************************************************************************
  3. ********************************************************************************
  4. ********************************************************************************
  5. ********************************************************************************
  6. ********************************************************************************
  7. ********************************************************************************
  8. ********************************************************************************
  9. ********************************************************************************
  10. ********************************************************************************
  11. ******************************************************************************** a;
Last edited by William Hemsworth; Dec 4th, 2008 at 1:13 pm.
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