我无法读取文件来确认用户名

时间:2019-04-12 11:03:58

标签: c++ fstream

我正在尝试制作一个程序,您必须在其中使用用户名登录(为简单起见,我先使用用户名,稍后会添加密码) 为了使其正常工作,我想将用户名写入文件(稍后再加密),并且要登录时,它将通过文件检查用户名是否正确。但是,当我输入错误的用户名时,它会自动登录,并且不要求创建新帐户。如果我确实使用正确的用户名登录,也是一样。我究竟做错了什么? (我很新,这是我的第一个像样的程序,所以如果我做错了明显的事情,请不要太苛刻。)

S。我做了一些我了解的事情。这就是我现在得到的: 但是,现在它不会将新的用户名写入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;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

它不会写入文件,因为一旦使用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返回有关操作是否成功的布尔值,以便您可以决定终止程序。例如。如果用户输入“否”。