1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#include <string>
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
class UserPw
{
public:
UserPw(string user, string password);//Constructor
string getUser(); // returns the user
string getPassword();
bool Checkpw(string user, string passwd); // returns true is user and password both match
private :
string user;
string password;
};
UserPw::UserPw(string user, string password)
{
this->user=user;
this->password=password;
}
string UserPw::getUser()
{
return user;
}
string UserPw::getPassword()
{
return password;
}
bool UserPw::Checkpw(string user, string passwd)
{
if(this->user == passwd)
{
return true;
}
else
{
return false;
}
}
class PasswordFile
{
public:
PasswordFile(string filename);// opens the file and reads the names/passwords in the vector entry
vector<UserPw> getFile(); // returns the vector entry
void addpw(UserPw newentry); //this adds a new user/password to entry and writes entry to the file filename
bool checkpw(string user, string passwd); // returns true if user exists and password matches
private:
string filename; // the file that contains password information
vector<UserPw> entry; // the list of usernames/passwords
void synch();
};
PasswordFile::PasswordFile(string filename)
{
string pass,user;
this->filename=filename;
ifstream infile;
getline(infile, user);
getline(infile, pass);
infile.open(this->filename.c_str());
while (infile.good())
{
entry.push_back(UserPw(user, pass));
getline(infile, user);
getline(infile, pass);
}
}
vector<UserPw> PasswordFile::getFile()
{
return entry;
}
void PasswordFile::addpw(UserPw newentry)
{
entry.push_back(newentry);
synch();
}
bool PasswordFile::checkpw(string user, string passwd)
{
if(user == passwd)
{
return true;
}
else
{
return false;
}
}
void PasswordFile::synch()
{
ofstream outfile;
outfile.open(this->filename.c_str());
for(int i=0; i<entry.size();i++)
{
outfile << entry[i].getUser() << " " << entry[i].getPassword() << endl;
}
}
int main()
{
PasswordFile passfile("password.txt");
passfile.addpw(UserPw("dbotting","123qwe"));
passfile.addpw(UserPw("egomez","qwerty"));
passfile.addpw(UserPw("tongyu","liberty"));
//write code the shows that passwords match usernames
}
|