1

Im doing a login/registration system and the thing Im struggling with is checking whether the password is correct. So I have a text file with the user data that looks like this.

email: email@email.com
username: user123
password: 1234

In order to check if the password given by the user is correct I need to compare it to the one in the file. So how do I extract the password from the *.txt file. I really have no idea where to start.

umok
  • 11
  • 2
  • do you know how to read the file line by line? if you do you need to manipulate the string after that? – Alessandro Teruzzi May 11 '21 at 14:34
  • ok thanks Im going to try that. should I find the part I want with .find() or is there another way? – umok May 11 '21 at 14:36
  • 1
    https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c Also, you should consider to store the encrypted password – Alessandro Teruzzi May 11 '21 at 14:58

3 Answers3

2

You could take a look at this answer I found: Reading file line by line
I think this can be a good start!

Then you can try to keep just the password: Get substring after specific character

Dharman
  • 30,962
  • 25
  • 85
  • 135
1

Reading a configuration file like that is something useful that you will quickly benefit from. Usually it is a pretty standard code where you iterate through all the lines of your file, reading key-value pairs and storing them (for example, in a std::map<std::string, std::string>), to use them later.

Here you have a commented example of how such code would be:

#include <fstream>  // ifstream
#include <sstream>  // stringstream
#include <iostream> // cout, endl
#include <iomanip>  // ws
#include <map>      // map

using namespace std;

int main(){
    map<string, string> configuration; 
    ifstream fin("your_file.txt");
    string line;
    
    while(getline(fin, line)){ // loop through every line in the file
        string key;
        string value;
        stringstream ss(line); // make a stream from the line
        getline(ss, key, ':'); // read key until :
        ss >> ws;              // ignore whitespaces
        getline(ss, value);    // read value until newline
        
        // Store them
        configuration[key] = value;
    }
    cout << "PW: " << configuration["password"] << endl;
    return 0;
}
Miguel
  • 2,130
  • 1
  • 11
  • 26
0

If the words "email" "username" and "password" are written in the text document, start by removing them so it looks like this.

email@email.com
user123
1234

Then if you put the text file in the same folder as the source, this basic program should be a start.

#include <iostream>
#include <fstream>
#include <string>



using std::cout;
using std::cin;
using std::ifstream;
using std::getline;
using std::endl;
using std::string;

int main()
{

ifstream input("info.txt");    //open the text document
string line;
//int lineCounter;


string mail;
string password;
string username;

cout << "Enter your mail: " << endl;
cin >> mail;

cout << "Enter your password: " << endl;
cin >> password;

int compareResult;

if (input.is_open())
{

    while(getline (input,line))    //loop through input file line by line
    {

        if (line.length() > 0)    //skip empty lines
        {
            //lineCounter++;    //keeps track of what line you're at
            compareResult = line.compare(mail);    //compare each line of the document with the entered mail
            if (compareResult == 0)    //if match..
            {
                getline (input,line);    //get next line (username)
                username = line;    //store username in a variable
                
                getline (input,line);    //get next line (password)
                compareResult = line.compare(password);    //compare password with entered password
                if (compareResult == 0)    //if match..
                {
                    cout << "Logged in as " << username << endl;
                }
            }

        }

    }

input.close();
}


return 0;
}

Just change the code to match the name of your textfile or vice versa, just tried it and got logged in as user123.

Next time you need to find the answer to something similar, search for something like "searching text file for a particular string c++"

I haven't done anything in c++ in ages, but this question was the first I saw after signing up so I felt compelled, THANK YOU!