登陆时密码不管怎么输入都是错误,请各位专家指导,急!

不知道是不是没有保存文件里面的原因,两次输入相同的密码也是显示密码错误,是代码错了还是别的原因,希望各位专家解答一下。

img

img

img

img

img

img

供参考:

#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct The_users
{
    char id[100];
    char password[20];
    char phone[12];
    char mailbox[100];
    struct The_users* next;
}users;
void add_users(char* s = NULL)
{
    FILE* fp;
    errno_t err;
    int count = 0, i = 0, flg = 0, flag = 0;
    char  name_id[100], password[20], phone[12], mailbox[100];

    if ((err = fopen_s(&fp, "users.txt", "a")) != 0)
        printf("文件打开失败!\n");
    else {
        if (s == NULL){
            printf("新建用户名:");
            scanf_s("%s", name_id, (int)sizeof(name_id));
        }
        else
            strcpy(name_id, s);
        do {
            printf("请输入密码(六位[数字+字符]):");
            scanf_s("%s", password, (int)sizeof(password));
            count = strlen(password);
            for (i = 0, flg = 0, flag = 0; i < count; i++)
                if (isdigit(password[i]))
                    flg++;
                else
                    flag++;
        } while (count != 6 || flg == 0 || flag == 0);
        printf("请输入电话:");
        scanf_s("%s", phone, (int)sizeof(phone));
        printf("请输入邮箱:");
        scanf_s("%s", mailbox, (int)sizeof(mailbox));
        fprintf(fp, "%s %s %s %s\n", name_id, password, phone, mailbox);
        fclose(fp);
        printf("恭喜,您的账号注册成功!\n");
    }
}
void registers()
{
    char  name_id[100], password[20], phone[12], mailbox[100];
    users* head, * tail, * p;
    FILE* fp;
    errno_t err;
    printf("欢迎来到注册界面!\n");
    if ((err = fopen_s(&fp, "users.txt", "r")) != 0){
        add_users();
    }
    else {
        head = NULL, tail = NULL, p = NULL;
        while (1)
        {
            if (fscanf(fp, "%s %s %s %s\n", name_id, password, phone, mailbox) != 4) break;
            p = (users*)malloc(sizeof(struct The_users));
            p->next = NULL;
            strcpy(p->id, name_id); strcpy(p->password, password);
            strcpy(p->phone, phone); strcpy(p->mailbox, mailbox);
            if (head == NULL)
                head = p;
            else
                tail->next = p;
            tail = p;
        }
        fclose(fp);
        do {
            printf("新建用户名:");
            scanf("%s", name_id);
            tail = head;
            while (tail != NULL && strcmp(tail->id, name_id) != 0)
            {
                tail = tail->next;
            }
            if (tail == NULL)
                add_users(name_id);
            else
                printf("该用户名已经被注册,请重新输入!\n");
        } while (tail != NULL);
    }
    return;
}
int login()
{
    char  name_id[100], password[20], phone[12], mailbox[100];
    users* head, * tail, * p;
    errno_t err;
    FILE* fp;
    int i = 0, flg = 0;
    if ((err = fopen_s(&fp, "users.txt", "r")) != 0){
        printf("无注册用户,请先注册用户。\n");
    }
    else {
        printf("欢迎来到登录界面\n");
        head = NULL, tail = NULL, p = NULL;
        while (1)
        {
            if (fscanf(fp, "%s %s %s %s\n", name_id, password, phone, mailbox) != 4) break;
            p = (users*)malloc(sizeof(struct The_users));
            p->next = NULL;
            strcpy(p->id, name_id);  strcpy(p->password, password);
            strcpy(p->phone, phone); strcpy(p->mailbox, mailbox);
            if (head == NULL)
                head = p;
            else
                tail->next = p;
            tail = p;
        }
        fclose(fp);
        printf("请输入用户名:");
        scanf_s("%s", name_id, (int)sizeof(name_id));
        tail = head;
        while (tail != NULL && strcmp(tail->id, name_id) != 0)
        {
            tail = tail->next;
        }
        if (tail != NULL)
        {
            do {
                printf("密 码:");
                scanf_s("%s", password, (int)sizeof(password));
                if (strcmp(tail->password, password) == 0)
                {
                    printf("登录成功!\n");
                    return 1;
                }
                else {
                    i++;
                    if (i != 3)
                        printf("输入密码错误,还有%d次机会,请重新输入!\n", 3 - i);
                    else {
                        printf("输入密码错误,3次机会已用完,退出登录!\n");
                        break;
                    }
                }
            } while (strcmp(tail->password, password) != 0);
        }
        else
            printf("该用户名不存在!\n");
    }
    return 0;
}
int main()
{
    int n;
    while (1)
    {
        printf("***********菜单*************\n");
        printf("**   1:登录              **\n");
        printf("**   2:注册              **\n");
        printf("**   3:退出              **\n");
        printf("***************************\n");
        printf("请输入1-3编号:");
        scanf_s("%d", &n);
        if (n == 2)
            registers();
        else if (n == 3)
            break;
        else if (n == 1)
            login();
    }
    return 0;
}