parsing delimited strings

This is a discussion on parsing delimited strings within the C++ Programming forums, part of the General Programming Boards category; I am trying to write an ANSI C++ compatible routine to parse a string such as the following that is ...

  1. #1
    Unregistered
    Guest

    Question parsing delimited strings

    I am trying to write an ANSI C++ compatible routine to parse a string such as the following that is delimited by a comma(,)
    123,456,abc,,def
    Into
    Field1=123
    Field2=456
    Field3=abc
    Field4=
    Field5=def

    I could use strtok, but it does not handle an empty token and
    returns Field4=def.

    I know that I could use some character by character logic to
    pick off the values, but I'm hoping there is something better
    available, while still using ANSI C++ standard code.

    Please help!!

  2. #2
    William
    Guest
    You can try using the string class with the algorithm function templates. These provide all you need to do that kind of stuff (find, count...).

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    Salem:

    Just out of curiosity, what value does endp + 1 have the first time through your for loop? Since endp is initialized to NULL and the value isn't changed until after the embedded if statement concludes is NULL + 1 a legal statement? The same would hold for the line:

    int num = p - endp - 1;

    the first time through the loop. is p - NULL - 1 a legal statement. I thought NULL was defined as 0 which is different from the integer value zero making both of these statements undefined, correct? (asked meekly)

  4. #4
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    endp + 1 is refering to the address which endp points to, not the value that address contains. To add 1 to the value which endp points to you need to use the * operator ie *endp++.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    Salem:

    gotcha, mostly.

    c_ coder pointed out part of my problem and I didn't realize when you use logical and, &&, the left hand side is evaluated first and the right hand side is evaluated only if the left equates to true. I thought both sides were evalutated and if both sides were true, then the statement resolves to true. Thanks.

    c_coder

    Thanks for pointing out the error of my ways. For a while I thought you were wrong and temporarily posted a message to that effect. But after thinking about it I see that you are correct. Using the edit function of the board allows me to apologize and correct my mistake at the same time.
    Last edited by guest; 11-08-2001 at 01:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parsing command line strings
    By John_L in forum C Programming
    Replies: 15
    Last Post: 05-28-2008, 09:26 AM
  2. sscanf and parsing strings
    By jco1323 in forum C Programming
    Replies: 4
    Last Post: 02-20-2008, 06:32 PM
  3. Parsing Strings
    By Hunter2 in forum C++ Programming
    Replies: 29
    Last Post: 12-05-2004, 07:48 PM
  4. Parsing Strings
    By SubLogic in forum C++ Programming
    Replies: 15
    Last Post: 01-07-2003, 11:11 AM
  5. Searching and Comparing Strings Using Parsing
    By niroopan in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2002, 11:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21