我无法从文件中读取以确认用户名

I cant read from files to confirm username

本文关键字:确认 用户 读取 文件      更新时间:2023-10-16

我正在尝试制作一个程序,您必须使用用户名登录(为了简单起见,我首先使用用户名,稍后我会添加密码(为了使它工作,我想将用户名写入一个文件(稍后我将加密(,当您要登录时,它将检查文件用户名是否正确。但是当我输入错误的用户名时,它会自动登录并且不会要求创建新帐户。如果我使用正确的用户名登录,也是如此。我做错了什么?(我是新手,这是我的第一个半体面的程序,所以如果我做错了什么,请不要太苛刻。

所以。我做了一些我理解的事情。这就是我现在得到的:但是,现在它不会将新用户名写入Usernames_and_passwords文件。我非常困惑...

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <fstream>
using namespace std;
string user_input;
string birth_year;
string user_age;
string current_user_profile, current_user_password;
string username, password;
string newprofile;
string makenewusername, makenewpassword;
void TalkToAi() { //VOID TALKTOAI
    while (true) {
        cout << "write something: ";
        cin >> user_input;
            transform(user_input.begin(), user_input.end(), user_input.begin(), ::tolower); //TRANSLATES ALL UPPERCASE LETTERS TO LOWER CASE SO THE SYSTEM CAN UNDERSTAND!
        cout << user_input << "n"; //###FOR TESTING PORPOSES!!!###
        //IF LIBRARY!
        if (user_input == "what's my age?" || user_input == "count my age" || user_input == "whats my age?") {
            //###CONTINUE HERE!!!###
        }
    }
}
void StartUp() { //VOID UPONSTARTUP (WHAT TO DO, ALSO READS DIFFRENT PROFILES AND PASSWORDS FOR LOGIN)
    cout << "what profile should i load?" << "n" << "profile name: ";
    cin >> current_user_profile;
    fstream Myfile;
    Myfile.open("Usernames_and_passwords.txt");
    if (Myfile.is_open()) {
        while (getline (Myfile, username) ) {
            if (username == current_user_profile) {
                cout << "n" << "Hello, " << username << "n";
                break;
                }
                }
        if (username != current_user_profile) {
            cout << "wrong username or username unfortunately not found.n";
            cout << "shall i create a new profile? Yes/No: ";
            cin >> newprofile;
            if (newprofile == "Yes" || newprofile == "yes") {
                cout << "new profile username: ";
                cin >> makenewusername;
                Myfile << makenewusername << endl;
            }
        }
    }
}

它不会写入文件,因为一旦你使用 getline(( 读完文件,就会设置 eof 标志。您需要使用以下方法打开文件:

Myfile.open("Usernames_and_passwords.txt",fstream::in | fstream::out | fstream::app);

这会告诉程序打开文件进行读取和写入。 fstream::app 告诉程序将文本附加到文件的末尾。

然后,为了在eof被击中后重置,您可以这样做

Myfile.clear();
Myfile.seekg(0, ios::beg);  

这将清除 eof 标志,并将指针移回文件的开头。在此之后,您可以写入文件。

其他几点评论:您的循环已断开:如果文件为空,它将不起作用,如果输入的用户名在第二行而不是第一行,它将写入重复的用户名。

以下是函数的修改版本:

void StartUp() { //VOID UPONSTARTUP (WHAT TO DO, ALSO READS DIFFRENT PROFILES AND PASSWORDS FOR LOGIN)
    cout << "what profile should i load?" << "n" << "profile name: ";
    cin >> current_user_profile;
    fstream Myfile; 
    Myfile.open("Usernames_and_passwords.txt",fstream::in | fstream::out | fstream::app);   
    if (Myfile.is_open()) {
        while (getline (Myfile, username) ) {
            if (username == current_user_profile) {
                cout << "n" << "Hello, " << username << "n";
                return;
                }
        }
        cout << "wrong username or username unfortunately not found.n";
        cout << "shall i create a new profile? Yes/No: ";
        cin >> newprofile;
        if (newprofile == "Yes") {
            cout << "new profile username: ";
            cin >> makenewusername;         
            Myfile.clear();
            Myfile.seekg(0, ios::beg);                  
            Myfile << makenewusername << endl;  
        }   
    }
}

我还建议让 StartUp 返回一个关于操作是否成功的布尔值,这样您就可以决定终止程序。 例如,如果用户输入"否"。