假的strcmp结果时字符串相等 [英] strcmp results in false when strings are equal

查看:103
本文介绍了假的strcmp结果时字符串相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code:

 如果(STRCMP(PCH,地图[我]。名称)== 0){
            的printf(平等\\ n);
            返回0;
        }

PCH 从文件中读取,地图[我]。名称有64的已知大小。
该字符串大小低于63的比较这两个字符串时比64小的伟大工程:

file11111111111111111111111111111111111111111111111111111111111

  file11111111111111111111111111111111111111111111111111111111111

一切是桃色和如预期的结果是相等的,但是当这两个(尺寸64)进行比较:

file111111111111111111111111111111111111111111111111111111111111

  file111111111111111111111111111111111111111111111111111111111111

返回的是假的。
我想这样做的:

 如果(STRNCMP(PCH,地图[I] .name和64)== 0){
            的printf(平等\\ n);
            返回0;
        }

和它的工作,为64确切大小的字符串,但字符串
较小的结果是随机的。
我在这里处理什么样的诡诈的?

编辑:这是充满code:

 的char * PCH;
    焦炭tempFilesNeeded [100 * 64 + 100];
    的strcpy(tempFilesNeeded,地图[I] .filesNeeded);
    PCH = strtok的(tempFilesNeeded,,);
    而(PCH!= NULL)
    {
        如果(STRCMP(PCH,地图[我]。名称)== 0){
            的printf(平等\\ n);
            返回0;
        }        PCH = strtok的(NULL,,);
    }


解决方案

好吧,如果这是

 字符PCH [64];

那么你就不能在那里有64个可见字符,因为需要终止的最后一项。如果你确实有file111111111111111111111111111111111111111111111111111111111111数组中,它没有终止,并要求的strcmp()它调用未定义行为

此外,作为一个小点,他说,的strcmp()返回假是错误的,因为它的回报的的布尔值。它返回两个第一不同字符之间的关系;如果没有字符不同的字符串相等,则返回零。

This is my code:

        if(strcmp(pch,map[i].name)==0){
            printf("Equal\n");
            return 0;
        }

pch is read from a file, map[i].name has a known size of 64. This works great for strings smaller than 64. when comparing these two strings below of size 63:

file11111111111111111111111111111111111111111111111111111111111 and

file11111111111111111111111111111111111111111111111111111111111

everything is peachy and the result as expected is equal, but when these two (of size 64) are compared:

file111111111111111111111111111111111111111111111111111111111111 and

file111111111111111111111111111111111111111111111111111111111111

the return is false. I thought of doing:

        if(strncmp(pch,map[i].name,64)==0){
            printf("Equal\n");
            return 0;
        }

And it does work for strings of exact size of 64, but for strings that are smaller the result is random. What kind of quirkiness am i dealing with here?

EDIT: this is the full code:

    char * pch;
    char tempFilesNeeded[100*64+100];
    strcpy(tempFilesNeeded,map[i].filesNeeded);
    pch = strtok(tempFilesNeeded,",");
    while (pch != NULL)
    {
        if(strcmp(pch,map[i].name)==0){
            printf("Equal\n");
            return 0;
        }

        pch = strtok (NULL, ",");
    }

解决方案

Well, if it's

char pch[64];

then you can't have 64 visible characters in there, since the last entry is needed for the termination. If you do have "file111111111111111111111111111111111111111111111111111111111111" in that array, it's not terminated and calling strcmp() on it invokes undefined behavior.

Also, as a minor point, saying that strcmp() returns "false" is wrong, since its return is not boolean. It returns the relation between the two first differing characters; if no characters differ the strings are equal, then it returns zero.

这篇关于假的strcmp结果时字符串相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆