943,624 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1334
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 2nd, 2008
0

find string in txt file

Expand Post »
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
Similar Threads
Reputation Points: -14
Solved Threads: 1
Junior Poster in Training
FTProtocol is offline Offline
99 posts
since May 2008
Dec 2nd, 2008
0

Re: find string in txt file

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: -14
Solved Threads: 1
Junior Poster in Training
FTProtocol is offline Offline
99 posts
since May 2008
Dec 2nd, 2008
0

Re: find string in txt file

>>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.)
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Dec 2nd, 2008
0

Re: find string in txt file

Lol, this isnt for an assignment. I code because i can not because i have to but this is what i have so far.

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: -14
Solved Threads: 1
Junior Poster in Training
FTProtocol is offline Offline
99 posts
since May 2008
Dec 2nd, 2008
0

Re: find string in txt file

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Dec 2nd, 2008
0

Re: find string in txt file

oh it works

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: -14
Solved Threads: 1
Junior Poster in Training
FTProtocol is offline Offline
99 posts
since May 2008
Dec 2nd, 2008
0

Re: find string in txt file

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

Re: find string in txt file

lol im not lucky it works 100%
Reputation Points: -14
Solved Threads: 1
Junior Poster in Training
FTProtocol is offline Offline
99 posts
since May 2008
Dec 4th, 2008
0

Re: find string in txt file

Click to Expand / Collapse  Quote originally posted by FTProtocol ...
lol im not lucky it works 100%
Then what's the point of this thread, anymore?
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Dec 4th, 2008
0

Re: find string in txt file

Quote 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)
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008

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:





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


Follow us on Twitter


© 2011 DaniWeb® LLC