Thread: string tok

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    string tok

    Hey guys can somebody help me debug this problem. For some reason it tokenizez the first character correctly but then messes all the rest of the characters and i end up with mumbo jumpbo bogus input where half of the input is only correct. Any idea's?


    Code:
    int freqInput(int *array)
    {
       
        /* declaration of variables*/
        char *delim = " ";
        char chInput[255];
        char *p = chInput;
        int count = 0;
        
        
        /* call to user input*/
        fgets(chInput, 255, stdin);
        
        /*check for buffer overflow*/
         if(chInput[strlen(chInput)-1] !='\n')
        {
              readRestOfLine();
              return 0;
    
        }
        else
        {
           /*replace enter with Null   '\0'  */
         
           chInput[strlen(chInput)-1] = '\0';
        }
    
        
    
        /*string tokenize*/
        /*checking for NULL*/
    
        
        
        /* checking for null character*/
        
     
    
       /* tokenize the start of the input with a space*/
    
        p = strtok(chInput, delim);
    
       /* get the rest of the input tokenized until
          there is none left then convert it to a
          int and place it each element of the array*/
    
        while(p != NULL)
        {
          array[count++] = (int) strtol(p, NULL, 10);
          p = strtok(NULL, delim);
        }
    
       
      
    
       
       
        /* pass it back to options.c*/
    
        return count;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Repeat calls to strtok should be called with NULL instead of the string.
    The strtok() function is used to isolate sequential tokens in a null-ter-
    minated string, str. These tokens are separated in the string by at
    least one of the characters in sep. The first time that strtok() is
    called, str should be specified; subsequent calls, wishing to obtain fur-
    ther tokens from the same string, should pass a null pointer instead.
    The separator string, sep, must be supplied each time, and may change
    between calls.
    See the man page


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    That's what he's been doing.
    I'm currently at work without a C compiler (the horror), but several things that you might want to look at is to define your delimiter array with [] instead of a pointer to a string literal and additionally you might want to use atoi() for conversion, since you cast to int anyway.

  4. #4
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    ok ill give that a go

  5. #5
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    Ok that seemed to fix it thanks for that. But can i ask why str tok dosnt like char *DELIM = '' "?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I could have sworn he had the array name there when I looked for it. Oh well.

    The delimiter doesn't have to be mutable. In fact, it shouldn't be modified at all by strtok at all. Since arrays degrade to pointers to their type when passed to functions anyway, this would serve no purpose (since it isn't modified by the function).

    I do wonder however, what he's passing to this function as an argument. He has down that it's an array of ints.
    Code:
    #include<string.h>
    #include<stdlib.h>
    #include<stdio.h>
    
    int freqInput(int *array)
    {
       
        /* declaration of variables*/
        char *delim = " ";
        char chInput[255];
        char *p = chInput;
        int count = 0;
        
        /* call to user input*/
        fgets(chInput, 255, stdin);
    
        p = strtok(chInput, delim);
    
       /* get the rest of the input tokenized until
          there is none left then convert it to a
          int and place it each element of the array*/
    
        while(p != NULL)
        {
          array[count++] = (int) strtol(p, NULL, 10);
          p = strtok(NULL, delim);
        }
        /* pass it back to options.c*/
    
        return count;
    }
    
    int main( void )
    {
        int array[10] = {0};
        int f, c;
        
        printf( "> " );
        fflush( stdout );
        f = freqInput( array );
        
        for( c = 0; c < f && c < sizeof array / sizeof array[0]; c++ )
            printf( "array[ &#37;d ] is %d\n", c, array[ c ] );
        
        return 0;
    }
    Quick test case shows it working fine for me. I suspect they're doing a few things wrong:

    1 - Not initializing the array they're passing it.
    2 - Not filling it completely.
    3 - Printing the entire array, not only the filled cells.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM