Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <fstream>
- using namespace std;
- class user
- {
- friend class db;
- string username;
- string password;
- string email;
- public:
- user(string u, string p, string e)
- {
- username = u;
- password = p;
- email = e;
- }
- ~user()
- {
- }
- };
- class db
- {
- string dbfilename;
- vector<user> users;
- public:
- ~db()
- {
- }
- int find_user(string u)
- {
- int x = users.size();
- for(int i=0;i<x;i++)
- {
- if(users[i].username == u)
- {
- return i;
- }
- }
- return -1;
- }
- bool add_user(string u, string p, string e)
- {
- if(!u.empty() && !p.empty() && !p.empty() && find_user(u) == -1)
- {
- user NewUsr(u,p,e);
- users.emplace_back(NewUsr);
- users.shrink_to_fit();
- return true;
- }
- return false;
- }
- db(string filename)
- {
- dbfilename=filename;
- fstream file;
- file.open(dbfilename.c_str());
- string cuvant, u, p, e;
- for(int i=0;file>>cuvant;i++)
- {
- u,p,e = "";
- if(i == 0)
- {
- u = cuvant;
- }
- if(i == 1)
- {
- p = cuvant;
- }
- if(i == 2)
- {
- e = cuvant;
- cout << "Reading user: "<< users.size() << "\n" \
- << "U: " << u << endl \
- << "P: " << p << endl \
- << "E: " << e << endl;
- add_user(u,p,e);
- i=-1;
- }
- }
- file.close();
- }
- void print()
- {
- int x = users.size();
- for(int i=0;i<x;i++)
- {
- cout << "[" << i << "]: " \
- << users[i].username \
- << " " << users[i].password \
- << " " << users[i].email << endl;
- }
- }
- bool update()
- {
- fstream file;
- int x = users.size();
- for(int i=0;i<x;i++)
- {
- if(i==0)
- {
- file.open(dbfilename.c_str(), ios::out);
- file << users[i].username << " " << users[i].password << " " << users[i].email << endl;
- file.close();
- }
- else
- {
- file.open(dbfilename.c_str(), ios::out|ios::app);
- file << users[i].username << " " << users[i].password << " " << users[i].email << endl;
- file.close();
- }
- }
- return 0;
- }
- bool delete_user(string u)
- {
- int position = find_user(u);
- if(position != -1)
- {
- users.erase(users.begin()+position);
- update();
- return true;
- }
- return false;
- }
- };
- int main()
- {
- db DB("Useri");
- //DB.print();//Listare utilizator
- //Adaugare utilizator.
- /*
- string u,p,e;
- cout << "Register new account:\n"\
- << "User: ";
- cin >> u;
- cout << "Pass: ";
- cin >> p;
- cout << "Mail: ";
- cin >> e;
- if(DB.add_user(u,p,e))
- {
- cout << "Registration successful!\n";
- }
- else
- {
- cout << "Registration Failed!\n";
- }*/
- //freeing vector
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement