-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp2.cpp
230 lines (214 loc) · 6.36 KB
/
p2.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cstring>
#include <climits>
using namespace std;
struct Alumno {
char codigo [5];
char nombre [11];
char apellidos [20];
char carrera [15];
int ciclo;
float mensualidad;
int nextdel;
};
class FixedRecord {
private:
string filename;
public:
FixedRecord(string filename) {
this->filename = filename;
ifstream file(filename, ios::binary);
file.seekg(0, ios::beg);
if(is_empty(file)) {
ofstream filex(filename, ios::app | ios::binary);
int posfdel = -1;
filex.write((char*) &posfdel, sizeof(int));
}
}
bool is_empty(ifstream& pFile ){
return pFile.peek() == ifstream::traits_type::eof();
}
vector<Alumno> load() {
ifstream file(filename, ios::binary);
vector<Alumno> result;
Alumno a;
try {
if(!file.is_open()) {
throw 20;
}
}
catch(int x) {
cout << "Error: " << x <<". No se pudo abrir el archivo" << endl;
}
file.seekg(sizeof(int), ios::beg);
while(!file.eof()) {
a = Alumno();
file.read((char*) &a, sizeof(Alumno));
result.push_back(a);
}
file.close();
return result;
}
void add(Alumno record) {
record.nextdel = -100;
ifstream filef(filename, ios::binary);
try {
if(!filef.is_open()) {
throw 20;
}
}
catch(int x) {
cout << "Error: " << x <<". No se pudo abrir el archivo" << endl;
}
int holepos;
filef.seekg(0, ios::beg);
filef.read((char*) &holepos, sizeof(int));
if(holepos == -1) {
ofstream file(filename, ios::app | ios::binary);
file.write((char*) &record, sizeof(Alumno));
file.close();
filef.close();
}
else {
int newhead;
ofstream file(filename, ios::in | ios::binary);
filef.seekg((holepos + 1) * sizeof(Alumno), ios::beg);
filef.read((char*) &newhead, sizeof(int));
file.seekp((holepos) * sizeof(Alumno) + sizeof(int), ios::beg);
file.write((char*) &record, sizeof(Alumno));
file.seekp(0, ios::beg);
file.write((char*) &newhead, sizeof(int));
file.close();
filef.close();
}
}
Alumno readRecord(int pos) {
ifstream file(filename, ios::binary);
try {
if(!file.is_open()) {
throw 20;
}
}
catch(int x) {
cout << "Error: " << x <<". No se pudo abrir el archivo" << endl;
}
Alumno record;
file.seekg(pos * sizeof(Alumno) + sizeof(int), ios::beg);
file.read((char*) &record, sizeof(Alumno));
file.close();
return record;
}
bool deleteRecord(int pos) {
ifstream filer(filename, ios::binary);
try {
if(!filer.is_open()) {
throw 20;
}
}
catch(int x) {
cout << "Error: " << x <<". No se pudo abrir el archivo" << endl;
}
filer.seekg(0, ios::beg);
int nextdel;
filer.read((char*) &nextdel, sizeof(int));
int nextdelpos;
filer.seekg((pos+1) * sizeof(Alumno), ios::beg);
filer.read((char*) &nextdelpos, sizeof(int));
if(nextdelpos != -100) {
cout << "Record with pos: " << pos << " has already been deleted.";
return false;
}
filer.close();
ofstream filew(filename, ios::in | ios::binary);
filew.seekp((pos+1) * sizeof(Alumno), ios::beg);
filew.write((char*) &nextdel, sizeof(int));
filew.seekp(0, ios::beg);
filew.write((char*) &pos, sizeof(int));
filew.close();
filer.seekg(0, ios::beg);
filer.read((char*) &nextdel, sizeof(int));
return true;
}
void printHeader() {
int result;
ifstream file(filename, ios::binary);
file.seekg(0, ios::beg);
file.read((char*) &result, sizeof(int));
cout << result << endl;
file.close();
}
};
void printAlumno(Alumno alumno) {
for(int j = 0; j < 5; j++) {
cout << alumno.codigo[j];
}
cout << " ";
for(int j = 0; j < 11; j++) {
cout << alumno.nombre[j];
}
cout << " ";
for(int j = 0; j < 20; j++) {
cout << alumno.apellidos[j];
}
cout << " ";
for(int j = 0; j < 15; j++) {
cout << alumno.carrera[j];
}
cout << " " << alumno.ciclo << " " << alumno.mensualidad << " " << alumno.nextdel << endl;
}
void printAlumnos(vector<Alumno> alumnos) {
for(int i = 0; i < alumnos.size() - 1; i++) {
printAlumno(alumnos[i]);
}
}
char* completeblankspaces(string str, int size) {
char* result = new char[size];
for(int i = 0; i < size; i++) {
result[i] = ' ';
}
for(int i = 0; i < str.length(); i++) {
result[i] = str[i];
}
return result;
}
Alumno crearAlumno(vector<string> data, int Ciclo, float Mens){
Alumno a;
strcpy(a.codigo, completeblankspaces(data[0], 5));
strcpy(a.nombre, completeblankspaces(data[1], 11));
strcpy(a.apellidos, completeblankspaces(data[2], 20));
strcpy(a.carrera, completeblankspaces(data[3], 15));
a.ciclo = Ciclo;
a.mensualidad = Mens;
return a;
}
int main() {
FixedRecord fr("datos2.bin");
cout << endl;
vector<string> reg = {"0008", "Nincol", "Quiroz Maquin", "computacion"};
Alumno nuevo1 = crearAlumno(reg, 8, 4000.05);
//se agrega 2 veces un 1er alumno
fr.add(nuevo1);
fr.add(nuevo1);
//se cargan e imprimen todos los alumnos
printAlumnos(fr.load());
//se borra el primer record
fr.deleteRecord(0);
cout << endl;
printAlumnos(fr.load());
cout << endl;
vector<string> reg2 = {"0009", "Franco", "Pacheco Espino", "computacion"};
Alumno nuevo2 = crearAlumno(reg2, 1, 8000.2);
cout << endl;
// se agrega un nuevo alumno (en donde se borro el record pos = 0)
fr.add(nuevo2);
// se buscan a los 2 records individualmente y se imprimer
printAlumno(fr.readRecord(0));
cout << endl;
printAlumno(fr.readRecord(1));
cout << endl;
// se imprimen todos los records
printAlumnos(fr.load());
}