从txt文件中读取用户名和密码

发布于 2024-11-03 06:01:50 字数 680 浏览 1 评论 0原文

我需要从 .txt 文件中提取用户名和密码,但我很难思考到底如何做到这一点。我会尝试打破这个局面。

  1. 打开文件
  2. 读入用户名
  3. 将用户名与用户输入进行比较
  4. 将密码与与用户名关联的用户输入进行比较
  5. 如果用户名和密码匹配,则返回 true 或 false 是

的,这是家庭作业。我正在学习如何使用 fstream,同时等待 USPS 运送我的班级 txt 书。非常感谢您的帮助!

这是我到目前为止所得到的:

bool User::check(const string &uname, const string &pass)
{
    //open the file

    fstream line;
    line.open("users.txt");

    //Loop through usernames
        //If a username matches, check if the password matches
}

users.txt 文件如下所示:

ali87   8422

ricq7   bjk1903

messi   buneyinnessi

mike    ini99ou

jenny   Y00L11A09

end

I need to pull usernames and passwords from a .txt file and I am having a difficult time wrapping my mind around how exactly to do this. I'll try to break this down.

  1. Open the file
  2. Read in the Usernames
  3. Compare a username against a user input
  4. Compare a password against a user input associated with the username
  5. return true or false if the username and passwords match

Yes, this is homework. And I am learning how to use the fstream while waiting for USPS to ship my class txt book. Your help is greatly appreciated!

Here is what I have so far:

bool User::check(const string &uname, const string &pass)
{
    //open the file

    fstream line;
    line.open("users.txt");

    //Loop through usernames
        //If a username matches, check if the password matches
}

The users.txt file looks like this:

ali87   8422

ricq7   bjk1903

messi   buneyinnessi

mike    ini99ou

jenny   Y00L11A09

end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

偏闹i 2024-11-10 06:01:50

我认为以下伪算法可能是您更好的选择:

  1. 输入用户名、密码
  2. 打开文件流到文件
  3. 搜索用户名匹配的流(如果未找到则退出)
  4. 如果找到,将加密输入密码与存储的加密密码。
  5. 如果找到,则返回成功,否则,“未找到用户名或密码不正确。”。

对于步骤 3,您可以将每个行缓冲区存储在一个字符串中,您可以将其存储在字符串容器中。
理想情况下,在此处理过程中,您可以将字符串拆分为用户名、密码对,然后将它们存储在 std::map 中;然后通过map.find(输入用户名)==输入密码访问它。

您不需要将地图存储超过登录过程的持续时间,然后您应该丢弃该地图(可能作为本地函数变量)。

如果你的程序确实有一个目的,那么这是理想的,否则,就让它工作吧:)。

I think the following pseudo-algorithm may be a better option for you:

  1. Input username, password
  2. Open file stream to file
  3. Search stream for username match (exit if non found)
  4. If found, compare encrypted input password against stored encrypted password.
  5. If found, return success, else, "No username found or password incorrect.".

For step 3, you would store each line buffer in a string, which you could store in a container of strings.
Ideally, during this processing, you might split the string into a username, password pair, and then store them in a std::map; and then access this via map.find(input username) == input password.

You shouldn't need to store the map for more than the duration of the login process, you should then discard the map (maybe as a local function variable).

If your program actually has a purpose, this is ideal, otherwise, just get it working :).

淤浪 2024-11-10 06:01:50

我包括了 iostream、fstream 和 cstring。并使用命名空间std

int main()
{
char login_password[20];
char stored_password[20];   
char login_username[20];
char stored_username[20];

fstream pull("users.txt",ios::in);
if (!pull) { 
    cout<<"File not loaded!"<<endl;
    return -1; 
}
cout<<"Username: ";
cin>>login_username;
while(strcmp(login_username,stored_username)){ 

//if login and stored usernames are equal, function strcmp returns 0,
//at first loop they are certainly not, so it is: while(1)

    pull>>stored_username;
    if(pull.eof()){   //if it is the end of file
        cout<<"Username does not exist. "<<endl;
        return -1;  
    }
}
pull>>stored_password; 

//since username and password are in the same line, password next to
//correctly inputted username is saved in stored_password

cout<<"Password: ";   
//now user enters password to confirm username
cin>>login_password;
while(strcmp(stored_password,login_password)){
    cout<<"Wrong password. "<<endl;
    cout<<"Try again: ";
    cin>>login_password;
}
cout<<"Login successful."<<endl;
return 0;
}

users.txt 如下所示:

  • Lena84 uzumymw
  • Doris20 kjkszpj

用户名和密码之间有一个空格。(也没有项目符号)

I included iostream, fstream and cstring. And using namespace std.

int main()
{
char login_password[20];
char stored_password[20];   
char login_username[20];
char stored_username[20];

fstream pull("users.txt",ios::in);
if (!pull) { 
    cout<<"File not loaded!"<<endl;
    return -1; 
}
cout<<"Username: ";
cin>>login_username;
while(strcmp(login_username,stored_username)){ 

//if login and stored usernames are equal, function strcmp returns 0,
//at first loop they are certainly not, so it is: while(1)

    pull>>stored_username;
    if(pull.eof()){   //if it is the end of file
        cout<<"Username does not exist. "<<endl;
        return -1;  
    }
}
pull>>stored_password; 

//since username and password are in the same line, password next to
//correctly inputted username is saved in stored_password

cout<<"Password: ";   
//now user enters password to confirm username
cin>>login_password;
while(strcmp(stored_password,login_password)){
    cout<<"Wrong password. "<<endl;
    cout<<"Try again: ";
    cin>>login_password;
}
cout<<"Login successful."<<endl;
return 0;
}

users.txt looks like this:

  • Lena84 uzumymw
  • Doris20 kjkszpj

there is one white space between username and password.(also without bullets)

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