首页 > 解决方案 > C++用户登录程序只读取文件的第一行

问题描述

void abc::loginUser()
{
    abc x;
    fstream file;
    char usr[25];
    string pwd = "",line;
    int ch;
    file.open("user.txt"),ios::in;
    cout << "User Login" << endl;
    cout << "Username: ";
    gets(usr);
    cout << "Password: ";
    file.seekg(0);
    while(ch = getch())
    { 
        if(ch == 13)
        {
            while(file)
            {
                file >> user.username >> user.password;
                if(strcmp(usr,user.username)==0)
                {
                    if(pwd != user.password)
                    {
                        file.close();
                        cout << "\nLogin Failed";
                        getch();
                        system("cls");
                        x.login();
                    }
                    else if(pwd == user.password)
                    {
                        file.close();
                        cout << "\nLogin Successful";
                        getch();
                        system("cls");
                        x.menuUser();
                    }
                }
                else if(strcmp(usr,user.username)!=0)
                {
                    file.close();
                    cout << "\nUser Not Registered";
                    getch();
                    system("cls");
                    x.login();
                }
                else
                {
                    file.close();
                    cout << "\nLogin Failed";
                    getch();
                    system("cls");
                    x.login();
                }
            }
        }
        else if(ch == 8)
        {
            if(pwd.length() > 0)
            {
                cout<<"\b \b";
                pwd.erase(pwd.length()-1);
            }
        }
        else
        {
            cout << "*";
            pwd += ch;
        }
    }
}

有谁知道为什么程序只读取 user.txt 文件的第一行?我似乎找不到解决此问题的方法,尝试了 file.read 方法,但它也不起作用。

我应该改变什么来改进我的代码吗?

大家能帮帮我吗,这里是初学者。

(对不起我的英语不好,不是我的主要语言)

标签: c++fileauthentication

解决方案


推荐阅读