我试图读取一个文件,并从右侧分开等号的左手侧,并查看左手侧是否为“HOME”。它发现第一个标记是“HOME”,但没有说明它是否等于“HOME”。基本上写在文件中的行是“HOME = /cs/nsf
”。使用strtok令牌识别()
怎么回事?
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")
}
}
}
向我们展示文件的内容。 –
你确定它没有打印“HOME”吗? – pstrjds
@ RichardJ.RossIII不必要的。 – 2012-10-24 18:13:19