CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Guest

    Parsing Comma Delimted Strings

    Is there an easy or standard way to parse a comma delimited stirng into a character array?


  2. #2
    Join Date
    Apr 1999
    Location
    Portland, OR, USA
    Posts
    29

    Re: Parsing Comma Delimted Strings

    Use the function strtok() to separate the string into substrings with the delimiter character being the comma character.

    For example:

    char string[] = "This string,is delimited,with,commas.";
    char seps[] = ",";
    char * token;

    token = strtok(string, seps); // Get the first token
    while (token)
    {
    printf("Found a token: %s\n", token);
    token = strtok(NULL, seps); // Get the next token
    }



    Your output should be:

    Found a token: This string
    Found a token: is delimited
    Found a token: with
    Found a token: commas.



    Hope this helps!

    =================================================
    Valerie L. Bradley
    Software Engineer
    Intel Corporation

    * All opinions expressed are mine and not those of my employer.

  3. #3
    Join Date
    Feb 2009
    Posts
    1

    Re: Parsing Comma Delimted Strings

    char string[80]=" ";
    char * dummy;
    char seps[] = ",";
    int k,l,m;
    float x,y,z;
    //
    fscanf(f_ptr1,"%s\n",string); //read a line: 1,1,1,E1,0.13060,-0.40193,-0.90631

    k = atol(strtok(string, seps));
    l = atol(strtok(string, seps)); // Get the first token
    m = atol(strtok(string, seps)); // Get the first token
    dummy = strtok(string, seps); // Get the first token
    x = atof(strtok(string, seps)); // Get the first token
    y = atof(strtok(string, seps)); // Get the first token
    z = atof(strtok(string, seps)); // Get the first token

    I found these codes can not read correctly and can not parsing correctly. Would you please help to fix the codes

    Thanks alots

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Parsing Comma Delimted Strings

    If I'm understanding correctly, all strtok calls after the first should have NULL as their first parameter.

  5. #5
    Join Date
    Nov 2001
    Posts
    251

    Re: Parsing Comma Delimted Strings

    Use the method posted by Valerie Bradley.
    Either process them as they're found, or save/push them into a string array for later.
    Last edited by Syslock; February 25th, 2009 at 02:13 AM.

  6. #6
    Join Date
    Jan 2008
    Location
    India
    Posts
    408

    Re: Parsing Comma Delimted Strings

    Quote Originally Posted by ;129587
    Is there an easy or standard way to parse a comma delimited stirng into a character array?
    Since you have posted in VC++ forum, I could suggest you go for CStringT::Tokenize.

    Also, look at this thread.
    Rate the posts which you find useful

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

Featured