strtok problem

This is a discussion on strtok problem within the C Programming forums, part of the General Programming Boards category; here is my code Code: str=strtok(string,">") while(str!=NULL) { str=strtok(NULL,">"); . . . . . //here again i want to token ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    90

    strtok problem

    here is my code
    Code:
    str=strtok(string,">")
    while(str!=NULL)
    {
    str=strtok(NULL,">");
    .
    
    .
    .
    .
    .
    //here again i want to token suppose 128&as so
    char *str1;
    str1=strtok(str,"&");
    }
    here my problem is once i got the value " 128 " my strtok(NULL,"> ") is pointing to "as" .
    but wht i want is my strtok should point to "string" .
    wht should i do
    thank u
    sree

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    Post a more complete example, including an actual string you wish to tokenise, and more code than just all the strtok() calls.
    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.

  3. #3
    Devil™
    Join Date
    Oct 2007
    Location
    IIT-Kharagpur, India
    Posts
    104
    Code:
    str=strtok(string,">")
    while(str!=NULL)
    {
    str=strtok(NULL,">");
    .
    
    .
    .
    .
    .
    //here again i want to token suppose 128&as so
    char *str1;
    str1=strtok(str,"&");
    }
    though I didnt understand what you wanted to do there..
    I think you are trying to change the delimiter after the first time call to strtok..
    well in that case you dont have to give first arg to strtok.. keep it NULL

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    90
    i will explain with example
    char *string="<fdsjfdsfssassd>nbjnj&asdas>dsfds>"
    now i want to delimit the string using ">"
    char *str=strtok(string,">");
    after tht if i found "&" then we delimit the string with "&"
    if(strchr(str,'&')!=NULL)
    str1=strtok(str,"&");
    after i will continue the process until all ">" removed
    so
    Code:
    char *string="<fdsjfdsfs&sassd>nbjnj>dsfds>"
    char *str=strtok(string,">");
    while(str!=NULL)
    {
    str1=strtok(NULL,">");
    if(strchr(str1,'&')!=NULL)
    str2=strtok(str1,"&"); //here is the problem
    
    }
    once the strtok with "& "executed . the "strtok(NULL,">");" is refrence the str1 string not str .
    thank u
    sree

  5. #5
    Devil™
    Join Date
    Oct 2007
    Location
    IIT-Kharagpur, India
    Posts
    104
    First thing i would say looking at this code is that it should give segfault.
    as the strtok takes char*, but you are giving it string constant.. strtok changes the string you give it but if its read only(string constant) it will throw segfault..
    Code:
    char *string="<fdsjfdsfs&sassd>nbjnj>dsfds>"; 
    /*this gives you a string constant. 
    this cant be changed in your code after declaration*/
    
    char string[]="<fdsjfdsfs&sassd>nbjnj>dsfds>"; 
    /*this gives you array of chars. and it can be changed in your code latter
    for now use this and not the first declaration*/
    hmm..
    so you want sub delimiters..
    strtok has sister function strtok_r
    the man page had an example there as far as I remember.. and that is exactly what you wanted..
    refer to your man pages
    Last edited by gibsosmat; 11-07-2007 at 02:45 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    1. strtok() is NOT re-entrant, so you can't use it to extract tokens from TWO strings at the same time. Use strtok_r() if you have it.

    2. Your string is a char constant, which on a lot of machines is in read-only memory, and will immediately get you a seg-fault.
    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.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    string is a string literal, you can't modify it (which strtok() does).

    Are you just trying to remove '>' ?
    Last edited by zacs7; 11-07-2007 at 02:43 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  2. Replies: 6
    Last Post: 04-28-2006, 01:06 PM
  3. Strange problem - strtok
    By AngKar in forum C Programming
    Replies: 7
    Last Post: 04-23-2006, 08:36 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM

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