-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileIO.cpp
More file actions
103 lines (94 loc) · 2.66 KB
/
fileIO.cpp
File metadata and controls
103 lines (94 loc) · 2.66 KB
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
#include "fileIO.h"
#include <fstream>
#include <map>
#include <string>
#include <vector>
using namespace std;
extern vector<Book> books;
extern map<string, User> users;
int Lib::readFrom(string path)
{
ifstream in;
in.open(path);
if(in.is_open())
{
try{
string str;
int mode = 0;
while(!in.eof())
{
getline(in,str);
if(!str.empty())
{
if(str == "USER")
{
mode = 1;
continue;
}
if(str == "BOOK")
{
mode = 2;
continue;
}
if(mode == 2)
{
string author,publisher,categoryNumber,remaining;
// getline(in,name);
getline(in,author);
getline(in,publisher);
getline(in,categoryNumber);
getline(in,remaining);
books.emplace_back(str,author,publisher,categoryNumber,stoi(remaining));
}
if(mode == 1)
{
string account,pwd,type;
// getline(in,name);
getline(in,account);
getline(in,pwd);
getline(in,type);
users[account] = User{str,account,stoul(pwd),User::UserType(stoi(type))};
}
}
}
in.close();
} catch (...)
{
return 2;
}
} else return 1;
return 0;
}
int Lib::read()
{
return readFrom("data.txt");
}
int Lib::saveTo(string path)
{
ofstream out;
out.open(path);
if(out.is_open())
{
try{
out << "USER" << "\n";
for(auto x : users)
{
out << x.second.name + "\n" + x.second.account + "\n" + to_string(x.second.pwd) + "\n" + to_string(x.second.type) + "\n";
}
out << "BOOK" << "\n";
for(auto x: books)
{
out << x.name + "\n" + x.author + "\n" + x.publisher + "\n" + x.categoryNumber + "\n" + to_string(x.remaining) + "\n";
}
out.close();
} catch(...)
{
return 2;
}
}else return 1;
return 0;
}
int Lib::save()
{
return saveTo("data.txt");
}