strtok reset

This is a discussion on strtok reset within the C Programming forums, part of the General Programming Boards category; I am VERY new to C so please bear with me. I have 2 variables being set from a longer ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    7

    strtok reset

    I am VERY new to C so please bear with me. I have 2 variables being set from a longer string using strtok. From what I understand, this function sets an internal variable of the current position.

    Is it possible to reset said variable in order to allow the strtok function to start to search from the start of the string again?

    I am coding a module for the Anope IRC Services system, and I am doing a command override, so that actions can be performed prior to Anope executing the regular stuff. This is the reason why I need to, in effect, reset this variable at the end of my module, so that Anope's own code can do as it normally does.

    Cheers

    Phil

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Just pass the string as the first argument to strtok() again instead of passing NULL.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    Code:
    char *  strtok ( const char * string, const char * delimiters );
    According to the standard, if the string parameter in strtok is not NULL it will start seaching from the start of the string for the first occurance of delimiter. It then overwrites it with a null character and returns a pointer to the token.

    After a call to strtok is made you can call it again with NULL for a string parameter, and it'll search for a delimeter from where the last function call found the something.

    But you can search from the start of the string again, sure. Just choose a different delimeter because otherwise strtok will return NULL.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    95
    Quote Originally Posted by lavinpj1
    Is it possible to reset said variable in order to allow the strtok function to start to search from the start of the string again?
    Phil
    Bare in mind, the seconed argument to strtok will be replaced
    by NULL's.
    The best thing to do is keep abuffer with the orignal string
    in it.

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, 05:48 PM
  2. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 10:38 PM
  3. converting string to integer, for further use
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 07-01-2005, 04:12 AM
  4. strtok tokenizing on spaces as well as my delimiter
    By snowblind37 in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2004, 01:39 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 07:56 PM

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