strtok question

This is a discussion on strtok question within the C Programming forums, part of the General Programming Boards category; i have a string "some string : something,something,something" is there another way to use strtok to get it to print ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    4

    strtok question

    i have a string

    "some string : something,something,something"

    is there another way to use strtok to get it to print

    something
    something
    something

    i tried using
    Code:
    strtok(string_buffer, ":");
    while (temp = strtok(NULL, ","))
    {
       printf("%s\n", temp);
    }
    and doesnt give the correct result.
    Thanks

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    TRy
    Code:
        char *p;
        char *string="some string :something,something,something";
        p=strchr(string,':');    
        p++;
        p=strtok(p,",");
        while(*p)
        {
             printf("%s\n",p);
             p=strtok('\0',",");
        }
        return 0;

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    > char *string="some string :something,something,something";
    String constants are read-only, strtok() modifies the string.

    Use
    char string[]="some string :something,something,something";
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    .
    Join Date
    Nov 2003
    Posts
    307
    Yup. My bad.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    4
    thanks for that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  2. a question about strtok.
    By Masterx in forum C++ Programming
    Replies: 24
    Last Post: 11-18-2008, 10:09 PM
  3. strtok question
    By neandrake in forum C++ Programming
    Replies: 1
    Last Post: 11-18-2003, 02:51 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

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