Thread: Don't quite understand how to use strtok()

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    15

    Don't quite understand how to use strtok()

    So I want to loop through a file line by line. Everytime there is a ':' in a line, put the string before it into label. (i know that right now label keeps getting reset without ever doing anything with it)



    Code:
    int search(FILE *inp) {  
      char line[80];
      char label[11];
      char *tokenPtr;
      char delim1 = ':';
      int lineNum = 1;
      int i = 0;
    
      while(fgets(line, sizeof line, inp) != NULL) {  //loop through each line
        for(i = 0; i < 11; i++)
            label[i] = '\0';
        if (line[0] != '#' && line[0] != ' ') {  //if line doesnt start with # or a space...
          tokenPtr = strtok(line, delim1);   //split at :
    
    //HOW DO I TAKE EVERYTHING BEFORE THE : AND ASSIGN IT TO label?
          //label = stdrup(tokenPtr);
            printf("%s\n", label);
          }
        }
        else if(line[0] == '\n') {
        continue;    //dont increase line number
        }
        lineNum++;
      }
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Take another look at the strtok function. I believe you have to use it twice to make it work the way you want. There should be shown an example in a decent book on 'C' in the 'String and Character Functions' chapter.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Your use of strtok looks fine to me. label is a char array, so it already has memory allocated for it, thus strdup is wrong. You only need to copy into it, so use strcpy.

    Also, instead of using
    Code:
    for(i = 0; i < 11; i++)
        label[i] = '\0';
    to clear the contents of label, do
    Code:
    memset(label, '\0', sizeof(label));

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Actually, I take it back. You do have a problem with strtok. You're passing it delim1, which is a char. strtok requires a char * (a string) for delimiters, so declare
    Code:
    char *delim1 = ":";

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    Quote Originally Posted by anduril462 View Post
    Actually, I take it back. You do have a problem with strtok. You're passing it delim1, which is a char. strtok requires a char * (a string) for delimiters, so declare
    Code:
    char *delim1 = ":";
    Thank you. Those fixes got it working. But why do I get this warning: passing arg 1 of `strtok' makes integer from pointer without a cast


    this is on the line
    Code:
    tokenPtr = strtok(line,delim1);

  6. #6
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Are you including string.h?
    Code:
    while(!asleep) {
       sheep++;
    }

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    that get rid of that warning, but created a new one.

    warning: passing arg 1 of `strtok' makes integer from pointer without a cast

  8. #8
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    I can duplicate your warning by not #including string.h; including it fixes the warning.
    Code:
    while(!asleep) {
       sheep++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strtok
    By cstudent in forum C Programming
    Replies: 15
    Last Post: 04-23-2008, 06:05 AM
  2. strtok
    By warney_out in forum C Programming
    Replies: 10
    Last Post: 12-18-2004, 05:26 AM
  3. strtok
    By ober in forum C Programming
    Replies: 6
    Last Post: 05-02-2002, 12:34 PM
  4. strtok
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-02-2002, 01:34 AM
  5. strtok
    By (TNT) in forum Windows Programming
    Replies: 4
    Last Post: 02-21-2002, 09:54 AM