943,813 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 568
  • C++ RSS
Jun 15th, 2009
0

ignoring to a specific sequence of characters

Expand Post »
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!!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
glahr32 is offline Offline
1 posts
since Jun 2009
Jun 15th, 2009
0

Re: ignoring to a specific sequence of characters

Try to adopt this improvisation:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 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:
Previous Thread in C++ Forum Timeline: installing GTK+ on Ubuntu
Next Thread in C++ Forum Timeline: burn a the whole file into CD ?





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


Follow us on Twitter


© 2011 DaniWeb® LLC