gpt4 book ai didi

In C, strcmp() is not returning 0 when it should(在C中,strcMP()在应该返回0的时候没有返回)

转载 作者:bug小助手 更新时间:2023-10-27 21:10:56 32 4
gpt4 key购买 nike



I am comparing user input of usernames and passwords. the string that are being compared to are being read in from a file. For whatever reason, it is appropriately comparing the usernames, but not the passwords.

我正在比较用户输入的用户名和密码。正在比较的字符串正在从文件中读入。无论出于何种原因,它都会适当地比较用户名,而不是密码。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const int MAX_SIZE = 100;

int main()
{
FILE *fp;
char *filename = "userdata.txt";
char arr[100][MAX_SIZE];

//open for writing
fp = fopen(filename, "r");

//verify open
if(fp == NULL)
{
printf("%s does not exist", filename);
return 0;
}

int index = 0;

//read file into array
while(fgets(arr[index], MAX_SIZE, fp) != NULL)
{
index++;
}

//username input
char username[100];
printf("Username: ");
scanf("%s", username);

//password input
char password[100];
printf("Password: ");
scanf("%s", password);


int check1 = 0;
int check2 = 0;
int x;
for (int i = 0 ; i<index ; i++)
{
char *token = strtok(arr[i], " ");

while (token != NULL)
{
x = strcmp(token,username);
printf("%d\n",x);
printf("%s %s\n",token,username);
if(!strcmp(token,username))
{
check1 = 1;
}

token = strtok(NULL, " ");
x = strcmp(token,username);
printf("%d\n",x);
printf("%s %s\n",token,password);
if(!strcmp(token,username))
{
check2 = 1;
}

token = strtok(NULL, " ");

if(check1&&check2)
{
printf("The amount is: %s\n",token);
return 0;
}

token = strtok(NULL, " ");

check1=0;
check2=0;
}
}
printf("Username/Password mismatch!!!\n");
return 0;
}


console output:

控制台输出:


Username: user1
Password: password1
0
user1 user1
-5
password1 password1
1
user2 user1
-5
password2 password1
2
user3 user1
-5
password3 password1
3
user4 user1
-5
password4 password1
4
user5 user1
-5
password5 password1
5
user6 user1
-5
password6 password1
Username/Password mismatch!!!

更多回答

What is the layout of the file being loaded? Without that detail, this code may be passing a NULL pointer to functions like strcmp() and printf().

正在加载的文件的布局是什么?如果没有这个细节,这段代码可能会将空指针传递给strcMP()和printf()等函数。

优秀答案推荐

When fgets reads in a line of text, it also reads and stores the newline at the end of the line.

当fget读入一行文本时,它还会读取并存储该行末尾的换行符。


This means that when you split the string with strtok and use " " as the delimiter, the read-in password includes the newline, while the password from the user read via scanf with the %s format specifier does not, causing the mismatch.

这意味着当您使用strtok拆分字符串并使用“”作为分隔符时,读入密码包括换行符,而通过scanf使用%S格式说明符读取的用户密码不包括换行符,从而导致不匹配。


You can fix this by including the newline character in the set of delimiters given to strtok.

您可以通过在给strtok的一组分隔符中包含换行符来修复此问题。


char *token = strtok(arr[i], " \n");
...
token = strtok(NULL, " \n");

Also, your second set of calls to strcmp is checking the username instead of the password. So instead of this:

此外,您对strcMP的第二组调用是检查用户名而不是密码。因此,与其这样,不如:


        x = strcmp(token,username);
printf("%d\n",x);
printf("%s %s\n",token,password);
if(!strcmp(token,username))

You want this:

你想要的是:


        x = strcmp(token,password);
printf("%d\n",x);
printf("%s %s\n",token,password);
if(!strcmp(token,password))

更多回答

Given that the OP's code suggests there is at least a 3rd parameter (amount) on the lines read from the file, the suggestion that the 2nd field of the line read with fgets() contains a LF is not likely to be appropriate.

假设OP的代码表明从文件中读取的行上至少有第三个参数(数量),那么使用fget()读取的行的第二个字段包含LF的建议可能不太合适。

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