Thread: strtok problem

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Taipei
    Posts
    70
    Rep Power
    12

    strtok problem


    #include<stdio.h>
    #include<string.h>

    void main(){
    char buffer[1024], *tok, *temp;
    FILE *openFile;

    openFile = fopen("c:/c++/a/example.mic", "r");
    tok = fgets(buffer, sizeof(buffer), openFile);

    while(tok !=NULL){
    temp = strtok(tok, " ");

    while(temp!=NULL){
    if(temp=="A") printf("A founded\n");
    temp = strtok(NULL, " ");
    }
    tok = fgets(buffer, sizeof(buffer), openFile);
    }
    }

    inside example.mic ==> B := A+3;

    Q:why it doesn't print "A founded"?
  2. #2
  3. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    because of how you are comparing temp to the string A.
    Try strcmp or strncmp instead.
    i.e.
    Code:
    if (!strcmp(temp, "A")) {
        printf("A found.\n");
    }
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Taipei
    Posts
    70
    Rep Power
    12
    thx.. it works

IMN logo majestic logo threadwatch logo seochat tools logo