Thread: strtok() question

  1. #1
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    strtok() question

    I am a little confused . I must misunderstand the use of strtok() function.

    Here is a snippet of code I am working with:

    Code:
    fgets(Temp,10,stdin);
    strtok(Temp, "\n");
    if(strcmp(Temp, "\n\0")==0) This line works as I expect and breaks the loop.
    //if(strcmp(Temp, "\0")==0)  //This line doesn't break the loop.
            break;
    I was assuming that if a user just pressed 'enter' for a blank line that strtok would replace the newline with NULL. What am I doing incorrectly.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The line that contains only "\n" will not contain any tokens, so strtok fails early.
    3 The first call in the sequence searches the string pointed to by s1 for the first character that is not contained in the current separator string pointed to by s2. If no such character is found, then there are no tokens in the string pointed to by s1 and the strtok function returns a null pointer. If such a character is found, it is the start of the first token.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You need to use the pointer that strtok() returns for your comparison instead of the original string. Here:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      char string[100];
      char *s;
    
      puts("Wrong method:");
      strcpy(string, "foo\nbar\nbaz\n");
      s = strtok(string, "\n");
      while(s)
      {
        puts(string);
        s = strtok(NULL, "\n");
      }
    
      puts("\nRight method:");
      strcpy(string, "foo\nbar\nbaz\n");
      s = strtok(string, "\n");
      while(s)
      {
        puts(s);
        s = strtok(NULL, "\n");
      }
    
      return 0;
    }
    Code:
    Wrong method:
    foo
    foo
    foo
    
    Right method:
    foo
    bar
    baz
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    Thank you for your input I'll mull this over a while until I understand this!

    Thanks again you are all great!
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



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. a question about strtok.
    By Masterx in forum C++ Programming
    Replies: 24
    Last Post: 11-18-2008, 11:09 PM
  3. strtok question
    By neandrake in forum C++ Programming
    Replies: 1
    Last Post: 11-18-2003, 03:51 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM