帮助!当提供 strtok 结果时,strcmp 对我撒谎

发布于 2024-08-18 22:28:25 字数 1323 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

穿透光 2024-08-25 22:28:25

当您比较的字符串相等时,strcmp 返回 0。您需要使用 if (!strcmp(...))

strcmp returns 0 when the strings you are comparing are equal. You need to use if (!strcmp(...)).

依 靠 2024-08-25 22:28:25

当字符串相等时,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

怀念你的温柔 2024-08-25 22:28:25

与您的问题无关,但有几点:

  • fread() 之后,您应该设置 buffer[fSize] = 0;,否则它不是字符串。
  • 使用 fseek(file,0,SEEK_END); 确定要在文本文件或二进制文件中读取的字符数; 不能保证正常工作

就我个人而言,我会将 malloc() 调用编写为:

buffer = malloc(fSize+1);

因为如果我忘记 #include,这会警告我,并且更易于阅读。 C 中不需要强制转换。

Not related to your question, but a couple of points:

  • After the fread(), you should set buffer[fSize] = 0;, otherwise it's not a string.
  • Determining the numbers of characters to read in either a text file or a binary file using fseek(file,0,SEEK_END); isn't guaranteed to work.

Personally, I would write the malloc() call as:

buffer = malloc(fSize+1);

because that will warn me if I forgot to #include <stdlib.h>, and is easier to read. The cast is not required in C.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文