Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jan 2005
    Posts
    74
    Thanks
    0
    Thanked 1 Time in 1 Post

    Question String Parsing using known delimiters? [C++]

    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:
    Code:
    class A
    {
    private:
    	// Record
    	string A
    	string B
    	long C;
    	string D;
    	string E;
    	string F;
    	double G;
    	int H;
    
    public:
    	A(string sLine);	// constructor
    };
    
    A::A(string sLine)
    {
    	// somehow parse the string here and determine if it is valid //
    }

    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,

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    You could do it with strtok. First tokenize based on the comma and then that will isolate the end part of the line with the numbers and tokenize that substring based on a space.
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •