Thread: comparing string to strtok

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    14

    comparing string to strtok

    Hello,
    Thanks for taking the time to help me out.

    Code:
    #include <stdio.h>
    int main()
    {
      //Open the file for reading
      FILE *in = fopen("CommandsProj1.dat", "r");
    
      //fgets buffer
      char buffer[100];
    
      //Pieces of string tokenized
      char * stringPiece;
    
    
      //While loop. Getting lines from file
      while (!feof(in)){
    
          fgets(buffer, 100, in);
          stringPiece = strtok (buffer," ");
    
          while (stringPiece != NULL){
                  printf("%s\n",stringPiece);
    
                  if (stringPiece=="H"){printf("Help");}
                
                  stringPiece = strtok (NULL, " ");
            }
    }
    
      //Close file
      fclose(in);
    
    
      return 0;
    }

    These 2 lines right here:
    Code:
                  printf("%s\n",stringPiece);
                  if (stringPiece=="H"){printf("Help");}
    When I print stringPiece it shows up as H but when I'm comparing it to "H" it doesn't seem that they are equal because I'm not getting the "Help" printed.

    Does strok() not return strings in this case?
    I am printing it with %s and its coming up correctly but when Im comparing it tells me they arent the same.
    How can I proceed to compare the result from strtok() to a string?
    (already tried the function strcmp() and have the same problem)

    Thanks!

    Lolo

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You need to use strcmp to compare strings. What ` (stringPiece=="H") ` does is compare the pointer value of stringPiece to the address of the string literal "H". That is, are they pointing to the exact same memory locations. But what if you had another string with the contents "H', stored elsewhere?
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You should also read the FAQ on why using feof() to control a loop is bad.

    All you need is
    while ( fgets(buffer, sizeof(buffer), in) != NULL )
    and this will loop through the file, reading 1 line at a time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    14
    Thanks for the reply guys!

    new code
    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    {
      //Open the file for reading
      FILE *in = fopen("CommandsProj1.dat", "r");
    
      //fgets buffer
      char buffer[100];
    
      //Pieces of string tokenized
      char * stringPiece;
    
      //int for comparing strings
      int compare=2;
    
    
      //While loop. Getting lines from file
      while ( fgets(buffer, sizeof(buffer), in) != NULL ){
          fgets(buffer, 100, in);
           // printf("%s\n", buffer);
    
          stringPiece = strtok (buffer," ");
          while (stringPiece != NULL){
    
    
                  printf("%s\n",stringPiece);
                  compare=strcmp(stringPiece,"H");
    
                  if (compare==0){printf("HELP");}
    
                  //printf("%s\n",stringPiece);
                  stringPiece = strtok (NULL, " ");
            }
    }
    
      //Close file
      fclose(in);
    
    
      return 0;
    }
    I put in the strcmp() function, and still;
    I print out stringPiece, it shows up as H but when I compare it, it doesn't seem to be the same.

    Am I doing something wrong with the compare?
    does the string from strtok() have a different structure or something than the "H"?

    Also,
    When I'm using
    while ( fgets(buffer, sizeof(buffer), in) != NULL )
    it only prints out halfway through the .dat file.
    (will keep on looking into that and try to fix it)

    Thanks for the time taken to help me out


    Lolo

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Remove the other fgets() from inside the loop!

    You're processing every other line at the moment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    14
    oops! thanks!
    That fixed the problem of not getting every line ^^

    But I'm still having the comparing string problem :S
    The one I get from strtok() prints the same but doesnt show up as the same as the one I inputted. How should I proceed?

    Thanks for your help

    Lolo

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If the H in your input file is immediately followed by a \n character, fgets() will place it in the string. strcmp("H\n", "H") does not return zero.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    14
    wow thanks mate! That worked for the H, which was immediately followed by a \n character.

    //How do I compare it to an H that would be followed by a space in that line? and then another character.
    never mind its comparing it normally now, thanks grumpy

    One more question though

    Im trying to use atoi() in one of the strings I got but its not working

    Code:
    stringPiece = strtok (buffer," ");
    int op1=atoi(stringPiece);
     printf("%d",op1);
    is this how I'm supposed to use it?>

    Thanks


    Thanks for your help

    lolo
    Last edited by loloxx; 03-23-2012 at 05:37 PM.

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    14
    Thanks for all the help, got it to work using atoi()

    =D

    end of thread

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 03-17-2012, 09:36 PM
  2. Simple Removal of string in string via strtok.
    By Vincent Wouters in forum C Programming
    Replies: 6
    Last Post: 11-11-2011, 01:48 PM
  3. loop in the same string using strtok
    By vitvar in forum C Programming
    Replies: 5
    Last Post: 10-27-2009, 07:14 AM
  4. strtok and string delimiters
    By Leonardo in forum C Programming
    Replies: 1
    Last Post: 05-01-2003, 04:28 PM
  5. Problem comparing string from text file with string constant
    By XenoCodex Admin in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 10:17 AM