943,834 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 767
  • C++ RSS
Jul 8th, 2009
0

String Parsing using known delimiters? [C++]

Expand Post »
The age old issue of string parsing comes up again ...
I have a text file that contains lines that are SUPPOSED to follow a set format, specifically:
string, string, long string int string double int

The delimiters are therefore:
Comma (,) for the first two fields
Spaces for all other fields

Strings like this would be valid:
Jon, Jack, 100 CPN 5 KTE 1.00 10
Jon, Jack 100 CPN 5 KTE 1.00 10 // notice the extra spaces

Whereas something like these would be considered invalid:
Jon Jack 100 CPN 5 KTE 1.00 10 // missing the commas
Jon, Jack, 100 CPN 5 KTE 1.00 // missing the last field "10"
Jon, Jack, 100CPN 5 KTE 1.00 10 // missing space between "100" and "CPN"

The goal is to EXTRACT each section and store them, and if possible determine when a string is INVALID (does not follow format).
I have a class with the following data members:
C++ Syntax (Toggle Plain Text)
  1. class A
  2. {
  3. private:
  4. // Record
  5. string A
  6. string B
  7. long C;
  8. string D;
  9. string E;
  10. string F;
  11. double G;
  12. int H;
  13.  
  14. public:
  15. A(string sLine); // constructor
  16. };
  17.  
  18. A::A(string sLine)
  19. {
  20. // somehow parse the string here and determine if it is valid //
  21. }


So, how can I parse the string (sLine) and extract each piece into there components (A, B, C, D, E, F, G, H)...
I was thinking of using the old method of simply doing substring searches but I find it very error prone and long ... is there a better way to accomplish this?

Anything anyone would recommend?
Any help would be much appreciated...
Thanks,
Similar Threads
Reputation Points: 9
Solved Threads: 0
Light Poster
Shaitan00 is offline Offline
38 posts
since Jul 2006
Jul 8th, 2009
0

Re: String Parsing using known delimiters? [C++]

What would your rule be for definingen when a token/line is invalid?
I would check the the number of tokens in each class is first 2, then 8

Or do you want a more elaborate check?
Last edited by monkey_king; Jul 8th, 2009 at 2:56 am.
Reputation Points: 70
Solved Threads: 9
Junior Poster
monkey_king is offline Offline
160 posts
since Aug 2008
Jul 8th, 2009
0

Re: String Parsing using known delimiters? [C++]

Shaitan00,

I think you are working on a code. It's too early to comment on your work.

>how can I parse the string>
Use find and substr methods.
Last edited by adatapost; Jul 8th, 2009 at 3:00 am.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jul 8th, 2009
0

Re: String Parsing using known delimiters? [C++]

You'll find this code snippet helpful
(If you're not allowed to use vectors, you can always adapt it to use an array)

Or use strtok.
Last edited by tux4life; Jul 8th, 2009 at 4:39 am.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jul 8th, 2009
0

Re: String Parsing using known delimiters? [C++]

Last edited by jencas; Jul 8th, 2009 at 9:17 am. Reason: added another link
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007

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: How to create a static map that is only loaded once? [C++]
Next Thread in C++ Forum Timeline: Questions on vector





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


Follow us on Twitter


© 2011 DaniWeb® LLC