/
Archivo secuencial indexado
198 lines (138 loc) · 3.65 KB
/
Archivo secuencial indexado
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
//============================================================================
// Name : proyecto.cpp
// Author : Franklin
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <fstream>
#include <cstdio>
using namespace std;
struct estudiante {
int carnet;
char nombre[15];
int edad;
char carrera[20]; };
struct index{
int carnet;
int posicion; };
int main(){
int opcion, bus;
estudiante alumno, auxi;
index indice, aux;
do{
ofstream dat("datos.txt", ios::binary | ios::app);
ofstream ind("index.txt", ios::binary | ios::app);
ifstream indic("index.txt", ios::binary);
cout<<"BIENVENIDOS"<<endl<<endl;
cout<<"Base de Datos UMG:"<<endl<<endl;
cout<<"1. Ingresar datos de estudiante"<<endl;
cout<<"2. Buscar datos de un estudiante"<<endl;
cout<<"3. Borrar registros"<<endl;
cout<<"4. Salir del programa"<<endl;
cout<<endl<<"Seleccione una opcion: ";
cin>>opcion;
cout<<endl;
if(opcion>0 && opcion<4){
switch(opcion){
case 1:
if(!dat && !ind){
cout<<"Error al abrir archivo"<<endl;
}else{
cout<<"Ingrese carnet: ";
cin>>alumno.carnet;
cout<<endl;
while(!indic.eof()){
indic.read((char*) &aux, sizeof(struct index));
if (alumno.carnet==aux.carnet){
cout<<"El archivo ya existe, Posicion: "<<aux.posicion;
cout<<endl;
bus=10;
}
}
indic.close();
if(bus!=10){
cout<<"Ingrese nombre: ";
cin>>alumno.nombre;
cout<<endl;
cout<<"Ingrese edad: ";
cin>>alumno.edad;
cout<<endl;
cout<<"Ingrese carrera: ";
cin>>alumno.carrera;
cout<<endl;
dat.write((char*) &alumno, sizeof(struct estudiante));
dat.seekp(0, ios::end);
indice.carnet=alumno.carnet;
indice.posicion=dat.tellp()/sizeof(struct estudiante);
ind.write((char*) &indice, sizeof(struct index));
cout<<"Guardado en posicion: "<<"["<<indice.posicion<<"]"<<endl;
dat.close();
ind.close(); }
}
case 2:
int buscar;
cout<<"Escriba carnet de alumno a buscar: ";
cin>>buscar;
while(!indic.eof()){
indic.read((char*) &aux, sizeof(struct index));
if (buscar==aux.carnet){
cout<<endl<<"POSICION: "<<aux.posicion;
cout<<endl<<endl;
ifstream dato("datos.txt", ios::binary);
bus=aux.posicion-1;
dato.seekg(sizeof(struct estudiante)*bus, ios::beg);
dato.read((char*) &auxi, sizeof(struct estudiante));
cout<<"Carnet: "<<auxi.carnet<<endl;
cout<<"Nombre: "<<auxi.nombre<<endl;
cout<<"Edad: "<<auxi.edad<<endl;
cout<<"Carrera: "<<auxi.carrera<<endl;
dato.close();
}
}
indic.close();
break;
case 3:
bus=0;
if(bus==0){
ifstream eliminar("datos.txt", ios::binary);
ifstream elim_ind("index.txt", ios::binary);
ofstream temporal("temp.txt", ios::binary | ios::out);
ofstream temp_ind("tempi.txt", ios::binary | ios::out);
if(!eliminar || !elim_ind){
cout<<"Error al abrir el archivo"<<endl;
}else{
cout<<"Ingrese el numero de carnet del alumno que desea borrar: ";
cin>>opcion;
while(!eliminar.eof()){
eliminar.read((char*) &auxi, sizeof(struct estudiante));
if(auxi.carnet==opcion){
cout<<"El archivo se ha eliminado de datos"<<endl;
}else{
temporal.write((char*) &auxi, sizeof(struct estudiante));
}
}
while(!elim_ind.eof()){
elim_ind.read((char*) &aux, sizeof(struct index));
if(aux.carnet==opcion){
cout<<"El archivo se ha eliminado de indice"<<endl;
}else{
temp_ind.write((char*) &aux, sizeof(struct index));
}
}
}
eliminar.close();
temporal.close();
elim_ind.close();
temp_ind.close();
}
remove("datos.txt");
remove("index.txt");
rename("temp.txt", "datos.txt");
rename("tempi.txt", "index.txt");
default: break;
}
}
} while(opcion>0 && opcion!=4);
}