ignoring to a specific sequence of characters

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

Join Date: Jun 2009
Posts: 1
Reputation: glahr32 is an unknown quantity at this point 
Solved Threads: 0
glahr32 glahr32 is offline Offline
Newbie Poster

ignoring to a specific sequence of characters

 
0
  #1
Jun 15th, 2009
I am trying to write a program that will open a specific file and go through it, writing the data I need to another file. Everytime the tag 'Step 4[2]' comes up I need to get the next two numbers and write them to a seperate file. (the purpose is to get the data to create a load displacement curve for an atomic simulation we're running) The results file is periodic, meaning that it repeats itself many times so the inefficient way would be to figure out how many lines are repeated and ignore that many before getting the data. The problem is, each simulation will have a different amount of lines needing to be skipped so it would take a lot of work to change the code for each different simulation. I'm hoping to write a more general code that can be used for all the simulations.

So here is my question: is there any way to ignore to a specific sequence of characters? (ie: ignore until it hits 'Step 4[2]')

I tried "inStream.ignore(4000, 'Step 4[2]');" but it give me errors because 'Step 4[2]' is more than one character. Is it possible to ignore until a specific sequence of characters instead of to a single character? Any help is GREATLY appreciated!!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: ignoring to a specific sequence of characters

 
0
  #2
Jun 15th, 2009
Try to adopt this improvisation:
  1. const size_t allbut = std::numeric_limits<int>::max();
  2. bool overStep(std::istream& in)
  3. {
  4. char c;
  5. while (in.ignore(allbut,'S')) {
  6. do
  7. if (in.get(c) && c == 't' &&
  8. in.get(c) && c == 'e' &&
  9. in.get(c) && c == 'p' &&
  10. in.get(c) && c == ' ' &&
  11. in.get(c) && c == '[' &&
  12. in.get(c) && c == '2' &&
  13. in.get(c) && c == ']')
  14. return true;
  15. while (in && c == 'S');
  16. }
  17. return false;
  18. }
  19. // mini-test
  20. int main()
  21. {
  22. std::ifstream fin("step.txt");
  23. std::string after;
  24. while (overStep(fin)) {
  25. if (fin >> after)
  26. std::cout << after << '\n';
  27. }
  28. return 0;
  29. }
Last edited by ArkM; Jun 15th, 2009 at 6:37 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