密码功能
我非常 *非常新的编程,尤其是使用C。我正在尝试创建一个可以从文件中读取用户名和密码的密码功能,并将其与输入的用户名和密码进行比较。问题在于,即使我输入的密码和用户名是正确的,代码即使在密码和用户名时也会打印错误消息。
void Passwrd ()
{
struct Password pssword[40];
char username1[20];
int passw0rd, c;
bool condition= false;
printf ("Hello. Please enter the username.\n");
scanf ("%s", &username1);
printf ("Hello. Please enter the password\n");
scanf ("%d",&passw0rd);
c=0;
FILE*password;
password= fopen ("Passwords.txt", "r");
while (condition== false ){
while(!feof(password)){
fscanf (password,"%s %d \n", &pssword[c].username,&pssword[c].passwrd);
if ((pssword[c].username==username1) && (pssword[c].passwrd== passw0rd)){
printf("Thank you! Please continue the program\n");
feof(password);
condition= true;
}
else if ((pssword[c].username!=username1) && (pssword[c].passwrd!=passw0rd)){
printf("Error!!The password or username is invalid\n");
condition= false;
c++;
}
}
fclose (password);
}
}
I am very *very new to programming especially using C. I'm trying to create a password function that can read the user name and password from a file and compare it to the username and password entered. The problem is that the code prints the error message even when the password and username I entered is correct.
void Passwrd ()
{
struct Password pssword[40];
char username1[20];
int passw0rd, c;
bool condition= false;
printf ("Hello. Please enter the username.\n");
scanf ("%s", &username1);
printf ("Hello. Please enter the password\n");
scanf ("%d",&passw0rd);
c=0;
FILE*password;
password= fopen ("Passwords.txt", "r");
while (condition== false ){
while(!feof(password)){
fscanf (password,"%s %d \n", &pssword[c].username,&pssword[c].passwrd);
if ((pssword[c].username==username1) && (pssword[c].passwrd== passw0rd)){
printf("Thank you! Please continue the program\n");
feof(password);
condition= true;
}
else if ((pssword[c].username!=username1) && (pssword[c].passwrd!=passw0rd)){
printf("Error!!The password or username is invalid\n");
condition= false;
c++;
}
}
fclose (password);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码不起作用的原因之一是您将字符串与
==
进行比较。字符串是char*
,它只是一个内存地址。使用==
时,您将比较内存地址。即使两个字符串相同,它们也不会在内存中同一位置,因此pssword [c] .username
不会==
用户名>
。要解决此问题,请使用
strcmp()
函数,函数,功能,甚至更好,strncmp()
。要注意的另一件事:您有逻辑错误。在您的
中,如果/else,如果
,您已经处理了发生的情况,如果是:如果一个是正确的, 条件将满足,因此不会打印任何消息。只需使用
else
而不是else如果在这种情况下
,则是这样的:另外,您可能需要考虑将变量重命名。使用
0
而不是o
和拼写密码
无法使其可读。也许类似recripe_password
,recript_user
,input [c] .password
和input [c] .user
将更容易跟随和调试。One reason why your code isn't working is that you're comparing strings with
==
. A string is achar*
, which is just a memory address. When using==
, you compare the memory addresses. Even if the two strings are the same, they won't be in the same place in memory, sopssword[c].username
will not==
username1
.To fix this, use the
strcmp()
function, or even better,strncmp()
.One other thing to note: you have a logic error. In your
if/else if
, you've handled what happens if:If one is right and the other is wrong, neither condition will be met, so no message will be printed. Just use
else
instead ofelse if
in this case, like so:Also, you may want to consider renaming your variables a bit. Using a
0
instead of ano
and misspellingpassword
doesn't make it readable. Maybe something likecorrect_password
,correct_user
,input[c].password
, andinput[c].user
would be easier to follow and debug.