-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.cpp
More file actions
158 lines (152 loc) · 3.62 KB
/
Copy pathFileSystem.cpp
File metadata and controls
158 lines (152 loc) · 3.62 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// This file includes all functions for file operation
#include<iostream>
#include<fstream>
#include<string>
#include<stdlib.h>
#include"DataStructure.h"
#include"Functions.h"
extern BookList * library;
extern ReaderList * readers;
int SaveLibrary() {
using namespace std;
BookNode * headBook = library->head;
unsigned int count = library->count;
ofstream lib("lib.data");
if (!lib) return 1;
lib << count << endl;
for (unsigned int i = 0; i < count; i ++) {
lib << headBook[i].book->id << "\t" << headBook[i].book->name << "\t" << headBook[i].book->author << "\t" << headBook[i].book->press << endl;
}
lib.close();
return 0;
}
int LoadLibrary() {
using namespace std;
BookNode * headNode = library->head;
Book * bookInfo;
char buff[100];
unsigned int count = 0, tempID = 0, curID = library->count;
string tempStr;
int ptrLoc, inputLoc = 0;
ifstream libData("lib.data");
if (!libData) return 1;
libData.getline(buff, 10);
for (int i = 0; i < 10; i++) {
if (buff[i] >= '0' && buff[i] <= '9') {
count = count * 10 + buff[i] - 48;
}
else
break;
}
for (unsigned int i = 0; i < count; i++) {
bookInfo = (Book*)malloc(sizeof(Book));
libData.getline(buff, 100);
for (ptrLoc = 0; ptrLoc < 100; ptrLoc++) {
if (buff[ptrLoc] != '\t') {
tempID = tempID * 10 + buff[ptrLoc] - 48;
}
else
break;
}
bookInfo->id = tempID;
ptrLoc++;
for (; ptrLoc < 100; ptrLoc++) {
if (buff[ptrLoc] != '\t') {
bookInfo->name[inputLoc] = buff[ptrLoc];
inputLoc++;
}
else
break;
}
bookInfo->name[inputLoc] = '\0';
ptrLoc++;
inputLoc = 0;
for (; ptrLoc < 100; ptrLoc++) {
if (buff[ptrLoc] != '\t') {
bookInfo->author[inputLoc] = buff[ptrLoc];
inputLoc++;
}
else
break;
}
bookInfo->author[inputLoc] = '\0';
ptrLoc++;
inputLoc = 0;
for (; ptrLoc < 100; ptrLoc++) {
if (buff[ptrLoc] != '\0') {
bookInfo->press[inputLoc] = buff[ptrLoc];
inputLoc++;
}
else
break;
}
bookInfo->press[inputLoc] = '\0';
inputLoc = 0;
headNode = (BookNode*)realloc(headNode, sizeof(BookNode)* (curID + 1));
headNode[curID].book = bookInfo;
tempID = 0;
curID++;
}
library->head = headNode;
library->tail = &(headNode[curID - 1]);
library->count += count;
return 0;
}
int SaveReaders() {
using namespace std;
Reader * headReader = readers->head;
ofstream rea("rea.data");
if (!rea) return 1;
rea << readers->count << endl;
for (int i = 0; i < readers->count; i++) {
rea << headReader[i].stdID << "\t" << headReader[i].name << endl;
}
rea.close();
return 0;
}
int LoadReaders() {
using namespace std;
Reader * headReader = readers->head;
char buff[100];
unsigned int count = 0, tempID = 0, curID = readers->count;
int inputPtr = 0, ptrLoc = 0;
ifstream rea("rea.data");
if (!rea) return 1;
rea.getline(buff, 10);
for (int i = 0; i < 10; i++) {
if (buff[i] >= '0' && buff[i] <= '9') {
count = count * 10 + buff[i] - 48;
}
else
break;
}
for (unsigned int i = 0; i < count; i++) {
headReader = (Reader*)realloc(headReader, sizeof(Reader) * (curID + 1));
rea.getline(buff, 100);
for (ptrLoc = 0; ptrLoc < 100; ptrLoc++) {
if (buff[ptrLoc] >= '0' && buff[ptrLoc] <= '9') {
tempID = tempID * 10 + buff[ptrLoc] - 48;
}
else
break;
}
headReader[curID].stdID = tempID;
ptrLoc++;
for (; ptrLoc < 100; ptrLoc++) {
if (buff[ptrLoc] != '\0') {
headReader[curID].name[inputPtr] = buff[ptrLoc];
inputPtr++;
}
else
break;
}
headReader[curID].name[inputPtr] = '\0';
inputPtr = 0;
tempID = 0;
curID++;
}
readers->head = headReader;
readers->count = count;
readers->tail = &(headReader[count - 1]);
return 0;
}