-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_1v2.cpp
115 lines (104 loc) · 2.51 KB
/
1_1v2.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
114
115
#include <fcntl.h>
#include <iostream>
#include <unistd.h>
#include <vector>
using namespace std;
/**
* @brief 单个对象的实例化
*
*/
class Serialize_single
{
private:
/* data */
int member;
public:
Serialize_single();
~Serialize_single();
void show_member(); //打印内容
//int member;
bool Serialize(const char *pFilePath);
bool Deserialize(const char *pFilePath);
};
Serialize_single::Serialize_single()
{
member = 1016; // last four digits of my student number
}
Serialize_single::~Serialize_single()
{
}
void Serialize_single::show_member(void)
{
std::cout << "member:" << member << std::endl;
}
/**
* @brief 序列化到文件中
*
* @param pFilePath 文件路径
* @return true 成功
* @return false 错误
*/
bool Serialize_single::Serialize(const char *pFilePath)
{
FILE *fp; //文件指针
if ((fp = fopen(pFilePath, "w+")) == NULL) //读写打开文件
{
std::cout << "open error!" << std::endl;
return false;
}
if (fwrite(&member, sizeof(member), 1, fp) <= 0) //写入序列化i,并判断写是否成功
{
std::cout << "write error!" << std::endl;
return false;
}
if (fclose(fp) == EOF) //文件关闭检查
{
std::cout << "close error!" << std::endl;
return false;
}
return true;
}
/**
* @brief 从文件中反序列化
*
* @param pFilePath 文件路径
* @return true 成功
* @return false 失败
*/
bool Serialize_single::Deserialize(const char *pFilePath)
{
FILE *fp; //文件指针
if ((fp = fopen(pFilePath, "r")) == NULL) //只读打开文件
{
std::cout << "open error!" << std::endl;
return false;
}
if (fread(&member, sizeof(member), 1, fp) <= 0) //写入序列化i,并判断写是否成功
{
std::cout << "read error!" << std::endl;
return false;
}
if (fclose(fp) == EOF) //文件关闭检查
{
std::cout << "close error!" << std::endl;
return false;
}
return true;
}
int main()
{
int flag1; //完成标识
{
Serialize_single cal;
cal.show_member();
flag1 = cal.Serialize("data");
std::cout << "serialize return:" << (flag1 ? "true" : "flase") << std::endl;
}
{
Serialize_single cal;
flag1 = cal.Deserialize("data");
cal.show_member();
std::cout << "Deserialize return:" << (flag1 ? "true" : "flase") << std::endl;
}
return 0;
}