【问题标题】:Strcmp does not do the comparingstrcmp 不做比较
【发布时间】:2015-11-01 15:59:39
【问题描述】:

为什么字符串之间的比较不起作用? 我确定user 字符串的末尾没有任何尾线,但我仍然知道用户名不被接受。

char user[24];
int userLog = -1;

FILE  *usernames;
usernames = fopen("usernames.cfg", "r");

if (usernames == NULL){
    perror("usernames - err");
    return(-1);
}

while(fgets(user, sizeof(user), usernames) !=NULL){

    strtok(user, "\n");
    printf("%s -- %s\n", user, possibleUsername);

    // First edition of question contained:
    // if((possibleUsername, user) == 0)
    // Still having problems with this version:
    if(strcmp(possibleUsername, user) == 0)
        userLog = 1;
    else 
        userLog = 0;

}

if(userLog == 1) printf("Username accepted:   %s\n", possibleUsername);
else if(userLog == 0) printf("Username doesn't exist in the database.\n");
fclose(usernames);

用户名.cfg:

user
justplayit
etc

【问题讨论】:

  • if((possibleUsername, user) 缺少 strcmp
  • 可以显示usernames.cfg的内容吗?如果它们总是在新行上,它们总是以换行符结尾,但 user 字符串不像你说的那样。这可能是问题所在。
  • 使用需要break;userLog = 1; --> { userLog = 1; break;}
  • @cad:由strtok 处理。 BLUEPIXY 做到了——即使有一个匹配,循环也会继续所有个名字。
  • 您的报告还应涵盖userLog == -1,如果数据库中还没有姓名,则会发生这种情况。您可以使用else 子句来覆盖userLog 中的任何其他值,尽管唯一可能的值是-1。

标签: c string strcmp


【解决方案1】:

应该是这样的

if(strcmp(possibleUsername, user) == 0)

因为表达式

(possibleUsername, user) == 0

等于

user == NULL

编辑

改变

int userLog = -1;

int userLog = 0;

然后删除

else
    userLog = 0;

【讨论】:

    【解决方案2】:

    试试这个:

    int main(int argc, char** argv)
    {   
        char user[24];
        int userLog;
        FILE* usernames;
        char* userPtr;
    
        usernames = fopen("usernames.cfg", "r");
        if (usernames == NULL)
        {
            perror("Usernames config not found or read protected");
            return EXIT_FAILURE;
        }
    
        while(fgets(user, sizeof(user), usernames) != NULL)
        {
            userPtr = strtok(user, "\n");
            if (userPtr != NULL) 
            {
                printf("Username in file => %s", userPtr);
                if (strcmp(userPtr, "find me") == 0)
                {
                    userLog = 1;
                    break;
                }
                else
                {
                    userLog = 0;
                }
            }
        }
    
        if (userLog)
        {
            printf("User find me accepted");
        }
        else
        {
            printf("User find me not in database");
        }
    
        fclose(usernames);
    
        return EXIT_SUCCESS;
    }
    

    它与您编写的程序相同,但我使用从 strtok 返回的额外指针来检查是否找到任何标记。将此令牌与“零终止”字符串(例如您的 possibleUsername 应该是)进行比较对我有用。如果可能用户名是一个固定长度的字符数组,建议您使用 strncmp 并设置要比较的字符串的长度,例如strncmp(userPtr, possibleUsername, length) == 0。如果 usernames.cfg 文件用 \r\n 保存,那么 strtok 将返回“username\r”而不是“username”,这也可能是问题所在。也许你可以调试你的程序并检查用户的缓冲区它有什么内容。希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多