I’m trying to read a file and separate left hand side of equal sign from right hand side and see if left hand side is “HOME”. It finds that first token is “HOME” but fails to say if it equals “HOME”. Basically the line written in file is “HOME = /cs/nsf“.
What’s going wrong?
while (fgets(buffer,80,file)) //while end of file
{
char *token = strtok(buffer,"=");
printf("first token: %s\n",token); //this correctly prints "HOME"
if (strcmp(token,"HOME") == 0)
{
printf("it doesn't get here");
token = strtok(NULL,"\n"); //this should return "/cs/nsf" but it doesn't;
if (token == NULL || strcmp(token," ") == 0)
{
fprintf(stderr,"HOME isn't initialised\n")
}
}
}
Your first printf prints
"HOME ". You should use"= \t"as your token string, to have it return"HOME"(and not any whitespace).