密码功能

发布于 2025-01-21 14:25:28 字数 1064 浏览 0 评论 0原文

我非常 *非常新的编程,尤其是使用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 技术交流群。

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

发布评论

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

评论(1

半边脸i 2025-01-28 14:25:28

您的代码不起作用的原因之一是您将字符串与==进行比较。字符串是char*,它只是一个内存地址。使用==时,您将比较内存地址。即使两个字符串相同,它们也不会在内存中同一位置,因此pssword [c] .username不会== 用户名>

要解决此问题,请使用 strcmp() 函数,函数,功能,甚至更好, strncmp()


要注意的另一件事:您有逻辑错误。在您的中,如果/else,如果,您已经处理了发生的情况,如果是:

  • 用户名和密码都是正确的,
  • 则用户名和密码都是错误的

如果一个是正确的, 条件将满足,因此不会打印任何消息。只需使用else而不是else如果在这种情况下,则是这样的:

if ((pssword[c].username==username1) && (pssword[c].passwrd== passw0rd)){
   printf("Thank you! Please continue the program\n");
   feof(password);           
   condition= true;
}
else {
  printf("Error!!The password or username is invalid\n");
  condition= false;
   c++;
}

另外,您可能需要考虑将变量重命名。使用0而不是o和拼写密码无法使其可读。也许类似recripe_passwordrecript_userinput [c] .passwordinput [c] .user将更容易跟随和调试。

One reason why your code isn't working is that you're comparing strings with ==. A string is a char*, 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, so pssword[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:

  • both the username and password are correct
  • both the username and password are wrong

If one is right and the other is wrong, neither condition will be met, so no message will be printed. Just use else instead of else if in this case, like so:

if ((pssword[c].username==username1) && (pssword[c].passwrd== passw0rd)){
   printf("Thank you! Please continue the program\n");
   feof(password);           
   condition= true;
}
else {
  printf("Error!!The password or username is invalid\n");
  condition= false;
   c++;
}

Also, you may want to consider renaming your variables a bit. Using a 0 instead of an o and misspelling password doesn't make it readable. Maybe something like correct_password, correct_user, input[c].password, and input[c].user would be easier to follow and debug.

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