-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
78 lines (66 loc) · 2.07 KB
/
Source.cpp
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
#include <fstream>
#include <iostream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/access.hpp>
using namespace std;
class Song {
};
class User {
public:
string username;
string password;
// Default constructor
User() {}
// Parameterized constructor
User(string a, string b) : username(a), password(b) {}
private:
// Serialization function
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar& username;
ar& password;
}
public:
void SignUp() {
//Serialization
{
std::ofstream ofs("user_data.txt");
boost::archive::text_oarchive oarchive(ofs);
User user1("john_doe", "password123");
User user2("alice", "secure_pass");
oarchive << user1 << user2;
cout << "User data serialized successfully." << endl;
}
}
void Login() {
{
std::ifstream ifs("user_data.txt");
boost::archive::text_iarchive iarchive(ifs);
try {
while (true) {
User loadedUser;
iarchive >> loadedUser;
if (loadedUser.username == username && loadedUser.password == password) {
cout << "Login Successful!" << endl;
break;
}
}
}
catch (const boost::archive::archive_exception& e) {
if (e.code == boost::archive::archive_exception::input_stream_error) {
cout << "End of file reached." << endl;
}
else {
cerr << "Error during deserialization: " << e.what() << endl;
}
}
}
}
};
int main() {
User u2("john_doe", "password123");
u2.Login();
}