Results 1 to 10 of 10

Thread: How to parse comma delimited string?

  1. #1

    Thread Starter
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157

    How to parse comma delimited string?

    Hi,
    I can read a comma delimited file and load the data into a string one line at a time. So, I can get something like...

    string s;
    s = "12.3, 14.5, 16.7";

    I can iterate through the string using s.substr and a 'for' loop...is there an equivalent to 'instr' command as in basic?

    Regards,
    ChuckB

  2. #2
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    std::string::find().

    Z.

  3. #3
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Though I believe that the boost library (http://www.boost.org/ ) has a handy string_tokenizer class that will do that work for you =).

    Z.

  4. #4

    Thread Starter
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157
    Z,
    Thanks for the link. They have a lot of samples using iterators...which is what I needed to see. Are you holding out on some other real usefull C++ sites? :-)
    Regards,
    ChuckB

  5. #5
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Don't overlook strtok() for char * -
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(){
    	char *whatever=
    	"this is an, example, showing parsing using, commas and spaces.";
    	char *p;
    	p=strtok(whatever,", "); /* find the first space */
            while(p){
                printf("%s\n",p);
    	    p=strtok('\0'," ,"); /* use spaces & commas*/
            }
       	return 0;
    }

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Just be bloody careful with where you point strtok -- it changes the string you pass in.

    Also, I don't think you can use strtok simultaneously on multiple strings.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    strtok doesn't change the string, but you can't use it on more than one string, that's right. And it is not thread-safe.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    strtok behaves differently on different boxes. Parksie is right - on HP UX 11.0, for example, it does change the source string.

    strtok_r uses a third argument - a char* - that stops strtok from clobbering the source.

    In MSVC++ it does not hurt the source.

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Quick aside for anyone else... the "_r" means "reentrant", which basically says that it's possible to call the function twice in the same process, at the same time (such as multiple threads). Reentrant functions don't have any static data, or anything like that.

    Well, they could. But they don't cause massive lossage if you do it
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Another side note: the strtok of MSVC++ in the dynamic or multithreaded static CRT library is thread-safe.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width