Thread: strtok giving empty strings

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

    strtok giving empty strings

    Hello everyone,
    I am having a bit of trouble getting strtok to tokenize my string. Here is the bit of code that is giving me trouble:
    Code:
      printf( "%s\n", temp );
      strcpy(h, strtok( temp, ":" ));
      strcpy(m, strtok( NULL, ":" ));
      strcpy(s, strtok( NULL, ":" ));
      printf( "%s,%s,%s\n",h, m, s );
    What comes out is this:
    Code:
    14:30:20.555
    14,,20.555
    That is, the first and third of the three substrings work perfectly but the middle one gives me an empty string when what I want is the "30".

    Why is it doing that, and how do I fix it?
    Thanks,
    H.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    My output is 14,30,20.555. Are you sure that you've recompiled your code???

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Can you post the entire code?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Sure, here is the entirety of my code

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main(void){
    
      char temp[]="14:30:20.555";
      char   h[3];
      char   m[3];
      char   s[6];
        
      printf( "%s\n", temp );
      strcpy(h, strtok( temp, ":" ));
      strcpy(m, strtok( NULL, ":" ));
      strcpy(s, strtok( NULL, ":" ));
      printf( "%s,%s,%s\n",h, m, s );
    
      return 0;
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I cannot duplicate the problem, but I do note that s should have a size of 7 instead of 6.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    char s[6]; /* 20.555 has 6 chars & no room for null byte declaring to 7 work @ least for me */

  7. #7
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Aha! That fixed it. How stupid of me. Why a missing \0 at the end of the third substring should affect the second, I'm not sure.

    Thanks everyone.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by TheBigH
    Why a missing \0 at the end of the third substring should affect the second, I'm not sure.
    One possibility is that that the position of s is before that of m in memory. strtok() writes to m, and then to s. But due to the lack of space for the null terminator, there is buffer overflow, and the null terminator is written to m[0], thus turning m into an empty string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    That makes a lot of sense, and would explain why you couldn't replicate the error: it's essentially random where the character arrays are stored so your m and s might have been in the other order.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can anyone help?
    By javalurnin in forum C Programming
    Replies: 11
    Last Post: 12-02-2009, 06:02 AM
  2. Help with binary file c++
    By lucky_mutani in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 09:24 AM
  3. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  4. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  5. array of strings + more
    By null in forum C Programming
    Replies: 10
    Last Post: 10-01-2001, 03:39 PM

Tags for this Thread