-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialization.cpp
More file actions
114 lines (91 loc) · 2.47 KB
/
serialization.cpp
File metadata and controls
114 lines (91 loc) · 2.47 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
104
105
106
107
108
109
110
111
112
113
114
//Boost serialization for CSCI 221 deadline project
//Example on complex STL data structures
//https://www.boost.org/doc/libs/1_71_0/libs/serialization/doc/index.html
//Mathew Nitz, Spring 2020
// vector < map<string, string> > deadlineVec;
// map<string, string> deadlineMap;
// deadlineMap.insert(pair<string, string>("title", "dateTime"));
// deadlineVec.push_back(deadlineMap);
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <boost/serialization/map.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
using namespace std;
class deadline
{
friend class boost::serialization::access;
vector < map<string, string> > deadlineVec;
string title;
string dateTime;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & deadlineVec;
}
public:
deadline(){};
deadline(string t, string dt) : title (t), dateTime (dt) {}
string getTitle() const;
string getDateTime() const;
vector < map<string, string> > & getCopy();
};
string deadline::getTitle() const
{
return this->title;
}
string deadline::getDateTime() const
{
return this->dateTime;
}
vector < map<string, string> > & deadline::getCopy()
{
return deadlineVec;
}
int main()
{
map<string, string> deadlineMap;
vector < map<string, string> > vecCopy;
deadline d("Walk the dog", "2020-05-24 18:00:00\n");
deadline e("Feed the cat", "2020-05-02 11:30:00\n");
deadline f("CSCI proj due", "2020-05-06 23:59:59\n");
vecCopy = d.getCopy();
deadlineMap.insert(pair<string, string>(d.getTitle(), d.getDateTime()));
deadlineMap.insert(pair<string, string>(e.getTitle(), e.getDateTime()));
deadlineMap.insert(pair<string, string>(f.getTitle(), f.getDateTime()));
vecCopy.push_back(deadlineMap);
ofstream ofs("./temp.txt");
{
boost::archive::text_oarchive oa(ofs);
oa << vecCopy;
}
vector < map<string, string> > newVecCopy;
{
ifstream ifs("./temp.txt");
boost::archive::text_iarchive ia(ifs);
ia >> newVecCopy;
}
//read from file
string line;
int linecount = 1;
string phrase = "22 serialization::archive 17 0 0 1 0 0 0 3 0 0 0";
int length = phrase.length();
ifstream file("./temp.txt");
if(file.is_open())
{
while(getline(file, line))
{
if(linecount == 1)
line = line.substr(length, line.length() - length);
linecount++;
cout << line << '\n' << endl;
}
file.close();
}
else cout << "Unable to read!" << endl;
return 0;
}