帮助!当提供 strtok 结果时,strcmp 对我撒谎
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, when fed the results of strtok, in the following code seems to be blatantly lying to 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 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;
}
With this input in a file in the parseList parameters:
job 23
job 10
Gives this output:
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
LIES!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您比较的字符串相等时,
strcmp
返回 0。您需要使用if (!strcmp(...))
。strcmp
returns 0 when the strings you are comparing are equal. You need to useif (!strcmp(...))
.当字符串相等时,strcmp 返回 0。
请参阅:http://www.elook.org/programming/c/strcmp.html< /a>
strcmp returns 0 when the strings are equal.
See: http://www.elook.org/programming/c/strcmp.html
与您的问题无关,但有几点:
fread()
之后,您应该设置buffer[fSize] = 0;
,否则它不是字符串。就我个人而言,我会将
malloc()
调用编写为:因为如果我忘记
#include
,这会警告我,并且更易于阅读。 C 中不需要强制转换。Not related to your question, but a couple of points:
fread()
, you should setbuffer[fSize] = 0;
, otherwise it's not a string.fseek(file,0,SEEK_END);
isn't guaranteed to work.Personally, I would write the
malloc()
call as:because that will warn me if I forgot to
#include <stdlib.h>
, and is easier to read. The cast is not required in C.