【问题标题】:Username not written when saving username and password to a file将用户名和密码保存到文件时未写入用户名
【发布时间】:2022-01-24 18:56:15
【问题描述】:

以下函数用于登录系统。它可以 100% 工作,除了变量“用户名”(用户输入)不写入任何内容/空白(数据丢失),而变量“密码”完美写入数据。

void login()
{
   while (1)
   {
       printf("Enter a selection (number 1 or 2), then press enter.\n\n");
       printf("1. Login\n2. Register\n\n");
       
       int selection = 0;
       scanf("%d", &selection);
       if (selection == 1)
       {
           FILE *fp;
           
           char username[24] = {'\0'};
           printf("\nEnter username:\n");
           fgets(username, sizeof(username), stdin);
           
           int c = 0;
           while ((c = getchar()) != '\n' && c != EOF);
           
           char password[24] = {'\0'};
           printf("\nEnter password:\n");
           fgets(password, sizeof(password), stdin);
           
           char extension[4] = ".txt";
           char fileName[strlen(username) + strlen(extension) + 1];
           strcpy(fileName, username);
           strcat(fileName, extension);
           
           fp = fopen(fileName, "r");
           
           if (fp != NULL)
           {
               char fileContents1[24] = {'\0'};
               char fileContents2[24] = {'\0'};
               for (int i = 0; i <= 1; i++)
               {
                    if (i == 0)
                    {
                        fgets(fileContents1, sizeof(fileContents1), fp);
                        
                        if (i == 1)
                        {
                            fgets(fileContents2, sizeof(fileContents2), fp);
                        }
                    }
                  
                    if ((username == fileContents1) && (password == fileContents2))
                    {
                        menu();
                    } else
                    {
                        printf("\nInvalid username or password, try again.\n\n");
                        continue;
                    }
               }
            } else 
            {
                printf("\nError, try again.\n\n");
                continue;
            }
           
            fclose(fp);
           
       } else if (selection == 2)
       {
           FILE *fp;
           
           char username[24] = {'\0'};
           printf("\nChoose a username:\n");
           fgets(username, sizeof(username), stdin);
           
           int c = 0;
           while ((c = getchar()) != '\n' && c != EOF);
           
           char password[24] = {'\0'};
           printf("\nChoose a password:\n");
           fgets(password, sizeof(password), stdin);
           
           char extension[4] = ".txt";
           char fileName[strlen(username) + strlen(extension) + 1];
           strcpy(fileName, username);
           strcat(fileName, extension);
           
           fp = fopen(fileName, "w");
           
           if (fp != NULL)
           {
               fputs(username, fp);
               fputs(password, fp);
               
               printf("\nLogin created successfully.\n\n");

           } else
           {
               printf("\nError, try again.\n\n");
               continue;
           }
           
           fclose(fp);
       } else
       {
           printf("\nInvalid selection, try again.\n\n");
           continue;
       }
   }
}

【问题讨论】:

  • username == fileContents1 - 这比较两个指针,而不是指向缓冲区中的内容。 -- 改为使用strcmp 函数。
  • 谢谢你,虽然该功能的“写入文件”部分仍然不起作用。
  • 请参阅fgets() doesn't work after scanf。在第一个 fgets() 之后,这个 while ((c = getchar()) != '\n' &amp;&amp; c != EOF); 可能会浪费 following 输入行,因为这里的换行符可能已经被读取。请熟悉各种输入函数如何处理包括换行符在内的空格。最好不要在同一代码中混用不同的输入函数。
  • 救命!非常感谢!

标签: c file-handling read-write


【解决方案1】:

至少有这些问题:

剩下的'\n'

scanf("%d", &amp;selection); 不消耗数字后面的任何内容,例如尾随的'\n'

fgets() 读取 '\n' - 很短的一行。

scanf("%d", &selection);
...
fgets(username, sizeof(username), stdin);

最好不要将scanf()fgets(...,..., stdin) 混用。

最好只使用fgets()

记住fgets() 读取并保存最后的'\n'

您可能希望在fgets() 之后删除潜在的'\n'

username[strcspn(username, "\n")] = '\0';

指针比较

username == fileContents1 比较两个字符串的地址。要比较字符串的内容,请使用strcmp()

可疑代码

fgets() 之后的int c = 0; while ((c = getchar()) != '\n' &amp;&amp; c != EOF); 仅在fgets() 未能读取整个 时才有意义。如果fgets() 这样做了,那么这个while() 会读取并抛出下一行。

【讨论】:

  • 非常感谢!
  • 欢迎来到 StackOverflow!我们通常通过投票和/或接受解决问题的答案来表示感谢。我建议您使用Tour(并获得您的第一个徽章)。
  • @coding0110:如果答案解决了您的问题,那么您可能需要考虑接受答案。有关更多信息,请参阅此官方帮助页面:What should I do when someone answers my question?
猜你喜欢
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 2019-06-11
  • 1970-01-01
  • 2013-12-27
  • 2011-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多