-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecordService.cpp
More file actions
104 lines (93 loc) · 2.28 KB
/
recordService.cpp
File metadata and controls
104 lines (93 loc) · 2.28 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
#define _CRT_SECURE_NO_WARNINGS
#include "recordService.h"
RecordService::RecordService() {
}
RecordService::~RecordService() {
}
bool RecordService::addNewid(std::string filename, int id) {
return this->writeContent(filename, std::to_string(id)+";");
}
//根据id在某一行追加
bool RecordService::addContentByid(std::string fileName, int id, std::string content) {
bool res = false;
std::ifstream in;
char line[1024] = { '\0' };
in.open(fileName);
int i = 0;
std::string tempStr;
while (in.getline(line, sizeof(line)))
{
i++;
tempStr += charToStr(line);
if (id == i)
{
tempStr += content+",";
res = true;
}
tempStr += '\n';
}
in.close();
std::ofstream out;
out.open(fileName);
out.flush();
out << tempStr;
out.close();
return res;
}
//根据id读取一行用户记录
std::string RecordService::readContentByid(std::string filename, int id) {
return this->readContentline(filename,id);
}
//根据id删除某条记录
bool RecordService::deleteContentByid(std::string filename, int id, std::string content) {
bool res = false;
std::ifstream in;
char line[1024] = { '\0' };
in.open(filename);
int i = 0;
std::string tempStr;
while (in.getline(line, sizeof(line)))
{
i++;
if(i!=id) tempStr += charToStr(line);
else
{
std::string restem = "";
std::vector<std::string> temp = split(charToStr(line), ";");
restem += temp[0]; restem += ";";
std::vector<std::string> temp1 = split(temp[1], ",");
for (int j = 0; j < temp1.size(); j++) {
if (temp1[j].compare(content) != 0) {
restem += temp1[j]; restem += ",";
}
}
tempStr += restem;
res = true;
}
tempStr += '\n';
}
in.close();
std::ofstream out;
out.open(filename);
out.flush();
out << tempStr;
out.close();
return res;
}
//所有的车站信息
std::vector<std::string> RecordService::getSite(std::string filename) {
std::vector<std::string> res;
std::string tempStr = readContent(filename);
std::vector<std::string> temp = split(tempStr,"\n");
/**/for (int i = 0; i < temp.size(); i++) {
std::vector<std::string> tmp = split(temp[i], ";");
if (tmp.size() > 1) {
tmp = split(tmp[1], ",");
for (int j = 0; j < tmp.size(); j++) {
std::vector<std::string> tmps = split(tmp[j], "#");
res.push_back(tmps[1]); res.push_back(tmps[2]); res.push_back(tmps[3]);
}
}
}
return res;
}