Thread: Problem with strtok at the end of a string.

  1. #1
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426

    Problem with strtok at the end of a string.

    Hi everyone,
    I am trying to write a little program that takes a line of comma-separated data from stdin, and insert a new entry inside it. Unfortunately it always segfaults when reading the last entry and I can't figure out why. Here's my code; can anyone help? I'm pretty sure the line I've marked with ***** is the culprit.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main(int argc, char *argv[]) {
       char line[1024], tokn[1024], outline[1024];
       int i, j=1;
    
       i = atoi( argv[2] );
       strcpy( outline, "" );
       fgets( line, 1024, stdin );
       printf( "%s,%d,%d\n", line, strlen(line), line[strlen(line)]=='\0' );             //make sure "line" is a proper, \0 terminated string, which it is
       if( i<=1 ) { 
          printf( "%s,%s", argv[1], line );    //Case when we have to put the desired string at the front
          return 0;
       }
    
       strcpy( tokn, strtok( line, ",\0" ) );
       while( 1 ) {                             
          if( i==j ) {strcat( outline, argv[1] );                     //have to put the string here
             strcat( outline, "," );                                  //append comma
          }
          strcat( outline, tokn );                                    //output token from original string
          strcat( outline, ","  );                                    //append comma
          strcpy( tokn, strtok( NULL, ",\0" ) );                      //get next token *****
          if(!tokn) break;                                            //break from while loop if end of string is reached
          printf( "%s\n", outline );
          fflush(stdout);                                             
          j++;
       }
       
       if( i>j ) {
          strcat( outline, argv[1] );   //Case when we have to put the desired string at the end
          strcat( outline, "," );
       } 
       outline[strlen(outline)-1] = '\0';         //remove trailing comma
       printf( "%s\n", outline );
       
       return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    strtok can, and will, return NULL when it is out of data; and you can't use NULL inside a strcpy. Do it in steps. Especially since you can't ever leave the loop: tokn is always a non-NULL address, so !tokn is never true.

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Ah, that makes sense. Fixed it. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop in the same string using strtok
    By vitvar in forum C Programming
    Replies: 5
    Last Post: 10-27-2009, 07:14 AM
  2. Understanding strtok from string.h library
    By yougene in forum C Programming
    Replies: 6
    Last Post: 03-07-2008, 01:14 AM
  3. turbo C: strtok function in <string.h>
    By kthouz in forum C Programming
    Replies: 2
    Last Post: 09-22-2007, 01:08 PM
  4. String to int using strtok and strtol
    By jtullo in forum C Programming
    Replies: 2
    Last Post: 05-06-2007, 04:37 PM
  5. strtok and string delimiters
    By Leonardo in forum C Programming
    Replies: 1
    Last Post: 05-01-2003, 04:28 PM

Tags for this Thread