Thread: looping with strtok()

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    173

    looping with strtok()

    I'm rather stumped on how to use strtok() in loops, it works fine for the first input and then 'dies' out on me for successive ones. Here's what I mean:

    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void tok_me()
    {
            char szString[BUFSIZ];
            int index = 0;
            char delims[] = " \n";
            char *result = NULL;
    
            while ((szString[index] = fgetc(stdin)) != '\n')
                    index++;
    
            result = strtok(szString,delims);
            while (result != NULL)
            {
                    printf("result is \"%s\"\n", result);
                    result = strtok(NULL,delims);
            }
    }
    
    int main(int argc, char **argv)
    {
            tok_me();
            tok_me();
            tok_me();
    
            return 0;
    }
    Basically it just breaks up the user input (ended with a \n) three times. I'm just experimenting with it before I put it in my larger program that requires successive tokenising and it has been giving me errors so I just went back to a simpler model (above).

    Heres a sample output of the program:
    Code:
    $ ./token1
    something 5 11
    result is "something"
    result is "5"
    result is "11"
    different 2
    result is "different"
    result is "2"
    result is "11"
    ah
    result is "ah"
    result is "ferent"
    Now I'm not sure what I'm doing wrong! But it seems to use the last strings (or part of it in the 3rd loop). Why does this occur? Doesn't the local variables get pushed off the stack? Or am I missing something here? I've already searched the forums for countless threads with none relating to this and I've already referred to man pages and c books to no avail!

    Any help or input would be greatly appreciated! Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > while ((szString[index] = fgetc(stdin)) != '\n')
    You're missing off the \0 to mark the end of the string.

    The fgets() function does exactly what you're trying to do, making sure not to overflow the buffer, and adding a \0 at the end.

    Other than that, it looks good.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    It works nicely now, thanks!
    Hopefully It'll work on the larger program

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, 09:38 PM
  3. converting string to integer, for further use
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 07-01-2005, 03: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, 12:39 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM