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 as %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;
}
将此输入放在parseList参数的文件中:
job 23
job 10
给出这个输出:
14 chars: reading buffer now:
**Running Token: job**
not job, job
End of state - job
**Running Token: 23**
Accepted 23 as job
End of state - 23
**Running Token: job**
not job, job
End of state - job
**Running Token: 10**
Accepted 10 as job
End of state - 10
所在!
答案 0 :(得分:6)
strcmp
返回0。您需要使用if (!strcmp(...))
。
答案 1 :(得分:1)
strcmp在字符串相等时返回0。 请参阅:http://www.elook.org/programming/c/strcmp.html
答案 2 :(得分:1)
与您的问题无关,但有几点:
fread()
之后,您应该设置buffer[fSize] = 0;
,否则它不是字符串。fseek(file,0,SEEK_END);
isn't guaranteed to work确定要在文本文件或二进制文件中读取的字符数。就个人而言,我会将malloc()
电话写为:
buffer = malloc(fSize+1);
因为如果我忘记了#include <stdlib.h>
,这会警告我,并且更容易阅读。 C中不需要演员。