String Parsing using known delimiters? [C++]

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

Join Date: Jul 2006
Posts: 35
Reputation: Shaitan00 has a little shameless behaviour in the past 
Solved Threads: 0
Shaitan00 Shaitan00 is offline Offline
Light Poster

String Parsing using known delimiters? [C++]

 
0
  #1
Jul 8th, 2009
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:
  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,
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 149
Reputation: monkey_king is on a distinguished road 
Solved Threads: 8
monkey_king monkey_king is offline Offline
Junior Poster

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

 
0
  #2
Jul 8th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,621
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 470
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

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

 
0
  #3
Jul 8th, 2009
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.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

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

 
0
  #4
Jul 8th, 2009
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.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

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

 
0
  #5
Jul 8th, 2009
Last edited by jencas; Jul 8th, 2009 at 9:17 am. Reason: added another link
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
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