c - Help! strcmp is lying to me when fed strtok results -
strcmp, when fed results of strtok, in following code seems blatantly lying me.
int fsize; char * buffer=null; char * jobtoken = "job"; char * nexttoken=null; job * curjob=null; struct node * head=null; struct node * parselist(file* file){     fseek(file,0,seek_end);     fsize=ftell(file);     buffer = (char*)malloc(fsize+1);     printf("%d chars: reading buffer now:\n",fsize);     fseek(file,0,seek_set);     fread (buffer,1,fsize,file);     nexttoken = strtok(buffer, " \n");     while (nexttoken!=null){             printf("**running  token: %s**\n",nexttoken);             if (strcmp(nexttoken,jobtoken)){                     printf("accepted %s  %s\n",nexttoken,jobtoken);                             }else{                     printf("not %s, %s\n",jobtoken,nexttoken);             }              printf("end of state - %s\n",nexttoken);              nexttoken = strtok(null, " \n");     }     free (buffer);     return null; }   with input in file in parselist parameters:
job 23 job 10   gives output:
14 chars: reading buffer now: **running  token: job** not job, job end of state - job **running  token: 23** accepted 23  job end of state - 23 **running  token: job** not job, job end of state - job **running  token: 10** accepted 10  job end of state - 10   lies!
strcmp returns 0 when strings comparing equal. need use if (!strcmp(...)).
Comments
Post a Comment