-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_1v1.cpp
90 lines (77 loc) · 1.5 KB
/
1_1v1.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
#include <iostream>
#include <fcntl.h>
#include <vector>
#include <unistd.h>
using namespace std;
class CA_LL
{
private:
/* data */
int i;
public:
CA_LL(/* args */);
~CA_LL();
void f(); //接口告知
//int member;
bool Serialize(const char *pFilePath);
bool Deserialize(const char *pFilePath);
};
CA_LL::CA_LL(/* args */)
{
i = 0;
}
CA_LL::~CA_LL()
{
}
void CA_LL::f(void)
{
std::cout << "set i:" << i << std::endl;
}
bool CA_LL::Serialize(const char *pFilePath)
{
int fd = open(pFilePath, O_RDWR | O_CREAT | O_TRUNC , S_IRUSR | S_IWUSR); //输出到文件
if (fd == -1) //检查打开是否正确
{
std::cout << "fd == -1" << std::endl;
return false;
}
if (write(fd, &i, sizeof(int)) == -1) //写入检查
{
close(fd);
return false;
}
if (close(fd) == -1) // 文件关闭检查
{
std::cout << "close error" << std::endl;
return false;
}
std::cout << "finish!" << std::endl;
return true;
}
bool CA_LL::Deserialize(const char *pFilePath)
{
int fd = open(pFilePath, O_RDWR); //输出到文件
if (fd == -1)
{
return false;
}
if (read(fd, &i, sizeof(int)) == -1)
{
close(fd);
return false;
}
if (close(fd) == -1)
{
return false;
}
return true;
}
int main()
{
CA_LL cal;
int flag1;
cal.f();
flag1 = cal.Serialize("data");
std::cout << "serialize return:" << flag1 << std::endl;
return 0;
}