gpt4 book ai didi

c - 帮助!当输入 strtok 结果时,strcmp 在骗我

转载 作者:太空宇宙 更新时间:2023-11-04 00:33:35 25 4
gpt4 key购买 nike

strcmp,当输入 strtok 的结果时,下面的代码似乎在公然骗我。

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

谎言!

最佳答案

当您比较的字符串相等时,

strcmp 返回 0。您需要使用 if (!strcmp(...))

关于c - 帮助!当输入 strtok 结果时,strcmp 在骗我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2114332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com