-
Notifications
You must be signed in to change notification settings - Fork 0
/
Save.cpp
113 lines (94 loc) · 2.38 KB
/
Save.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
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
#include "stdafx.h"
#include "Save.h"
std::string NumberToString (int number) {
std::ostringstream ss;
ss << number;
return ss.str();
}
void update_users_file(std::string user) {
std::fstream fs;
fs.open ("users.txt", std::fstream::in | std::fstream::out | std::fstream::app);
fs << user << std::endl;
fs.close();
}
void save_user_data(game_data data, int userid) {
std::string path = "Users\\";
std::string file = NumberToString(userid);
path.append(file.c_str());
std::ofstream out(path.c_str());
out << data.level << std::endl;
out << data.life << std::endl;
out << data.chosen_level << std::endl;
out << data.name << std::endl;
out << data.guns << std::endl;
out.close();
}
void load_user_data(game_data *data, int userid) {
// open file for reading
std::string path = "Users\\";
std::string line;
std::string file = NumberToString(userid);
path.append(file);
std::ifstream infile(path.c_str());
//get level
std::getline(infile, line);
data->level = atoi(line.c_str());
//get life
std::getline(infile, line);
data->life = atoi(line.c_str());
//get chosen_level
std::getline(infile, line);
data->chosen_level = atoi(line.c_str());
//get name
std::getline(infile, line);
strcpy(data->name, line.c_str());
//get guns
std::getline(infile, line);
data->guns = atoi(line.c_str());
infile.close();
}
void load_user(game_data *data, std::string user, int* u_id) {
int id;
BOOL user_found = FALSE;
int i = 0;
std::string line;
std::fstream infile("users.txt");
std::ofstream outfile("out.txt");
while (std::getline(infile, line)) {
if (line.compare(user) == 0) {
user_found = TRUE;;
break;
}
i++;
}
*u_id = i;
id = *u_id;
if (user_found == TRUE) {
outfile << "user found \n";
// read saved data from file
load_user_data(data, id);
}
else {
outfile << i << std::endl;
// write te name in users.txt
update_users_file(user);
// write data in user's file
std::string path = "Users\\";
std::string userid = NumberToString(i);
path.append(userid);
outfile << path << std::endl;
std::ofstream newfile(path.c_str());
newfile << 0 << std::endl;
newfile << 99 << std::endl;
newfile << 0 << std::endl;
newfile << 0 << std::endl;
newfile << user << std::endl;
data->level = 0;
data->life = 99;
data->guns = 0;
data->chosen_level = 0;
strcpy(data->name, (user.c_str()));
}
infile.close();
outfile.close();
}