ARCHIVOS con String.
Publicado por Andres Hernandez (1 intervención) el 05/03/2019 20:10:14
Hola buenas tardes, necesito poner los formatos de texto de tipo char, como tipo String. Y que los lea y escriba como string, use la función cin.getline para el backslash 0, pero aún así me devuelven el archivo donde me lo piden.
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <iostream>
#include <fstream>
#include <string.h>
#include <conio.h>
#include <stdio.h>
#define N 30
#define A 100
using namespace std;
struct Estudiante{
char cod[N];
char ape[N];
char nom[N];
char mat[N];
float n1, n2, n3;
};
Estudiante e[A], aux;
void ingresar();
void general();
void individual();
void modificar();
void eliminar();
bool buscar(char code[20]);
float promedio();
bool est();
ofstream ingresodat;
ifstream leodat;
ofstream editodat;
void ingresar(){
string cod;
system("cls");
int op=1;
//ABRIR EL ARCHIVO.
ingresodat.open("PROGRAMAPARCIAL.txt", ios::app);
if(ingresodat.fail()){
cout<<"\t ** EL ARCHIVO NO SE PUDO CREAR **"<<endl;
}
else{
while(op==1){
cout<<"\t ** Codigo del estudiante **: ";
cin>>e[A].cod;
if(!buscar(e[A].cod)){
cout<<"Ingrese primer y segundo nombre: ";
cin.getline(e[A].nom, 50);cin.getline(e[A].nom, 50);
cout<<"Ingrese apellidos completos: ";
cin.getline(e[A].ape, 50);
cout<<"Ingrese materia y codigo de materia: ";
cin.getline(e[A].mat, 50);
cout<<"Ingrese primer corte: ";
cin>>e[A].n1;
cout<<"Ingrese segundo corte: ";
cin>>e[A].n2;
cout<<"Ingrese tercer corte: ";
cin>>e[A].n3;
cout<<"Definitiva de la asignatura: "<<promedio()<<endl;
ingresodat<<e[A].cod<<" "<<e[A].nom<<" "<<e[A].ape<<" "<<e[A].mat<<" "<<e[A].n1<<" "<<e[A].n2<<" "<<e[A].n3<<endl;
}else{
cout<<"\t ** EL CODIGO YA EXISTE **"<<endl;
}
cout<<"\t **Desea seguir registrando? 1/0 (1 PARA SI, O PARA NO.)**"<<endl;
cin>>op;
system("cls");
}
ingresodat.close();
}
}
bool buscar(char code[20]){
bool find = false;
leodat.open("PROGRAMAPARCIAL.txt", ios::out|ios::in);
if (leodat.is_open()){ //Devuelve un valor no nulo si el fichero está abierto.
leodat>>aux.cod;
while (!leodat.eof()){ //EOF, mientras el archivo no finalice.
leodat>>aux.nom>>aux.ape>>aux.mat>>aux.n1>>aux.n2>>aux.n3;
if(strcmp(code,aux.cod) == 0){ //COMPARAMOS CADENAS CON EL STRCMP.
find = true;
}
leodat>>aux.cod;
}
leodat.close();
}
else{
cout <<"\t **La libreria no existe **"<< endl;
return find;}
}
void general(){
system("cls");
leodat.open("PROGRAMAPARCIAL.txt", ios::out|ios::in);
if(leodat.is_open()){ //Devuelve un valor no nulo si el fichero está abierto.
leodat>>e[A].cod;
while (!leodat.eof()){
leodat>>e[A].nom;
leodat>>e[A].ape;
leodat>>e[A].mat;
leodat>>e[A].n1;
leodat>>e[A].n2;
leodat>>e[A].n3;
cout<<"----------------------------------"<<endl;
cout<<"Codigo del Estudiante: "<<e[A].cod<<endl;
cout<<"Nombres del Estudiante: "<<e[A].nom<<endl;
cout<<"Apellidos del Estudiante: "<<e[A].ape<<endl;
cout<<"Materia y Codigo de Asignatura: "<<e[A].mat<<endl;
cout<<"Primer Corte: "<<e[A].n1<<endl;
cout<<"Segundo Corte: "<<e[A].n2<<endl;
cout<<"Tercer Corte: "<<e[A].n3<<endl;
cout<<"Definitiva: "<<promedio()<<endl;
cout<<"Estado: "<<((est()) ? "APROBADO" : "REPROBADO" )<<endl;
cout<<"----------------------------------"<<endl<<endl;
leodat>>e[A].cod;
}
leodat.close();
system("pause");
cout<<"PULSE UNA TECLA PARA BORRAR PANTALLA(...)"<<endl;
system("cls");
}
else{
cout<<"\t **La libreria no existe ** " << endl;}
}
void individual(){
system("cls");
bool find = false;
cout<<"\t ** Busqueda individual **: "<<endl;
cout<<"Ingrese el codigo del estudiante: ";
cin>>aux.cod;
leodat.open("PROGRAMAPARCIAL.txt", ios::out|ios::in);
if (leodat.is_open()){
leodat>>e[A].cod;
while (!leodat.eof()) {
leodat
>>e[A].nom
>>e[A].ape
>>e[A].mat
>>e[A].n1
>>e[A].n2
>>e[A].n3;
if(strcmp(aux.cod,e[A].cod) == 0){
cout<<"----------------------------------"<<endl;
cout<<"Codigo del Estudiante: "<<e[A].cod<<endl;
cout<<"Nombres del Estudiante: "<<e[A].nom<<endl;
cout<<"Apellidos del Estudiante: "<<e[A].ape<<endl;
cout<<"Materia y Codigo de Asignatura: "<<e[A].mat<<endl;
cout<<"Primer Corte: "<<e[A].n1<<endl;
cout<<"Segundo Corte: "<<e[A].n2<<endl;
cout<<"Tercer Corte: "<<e[A].n3<<endl;
cout<<"Definitiva: "<<promedio()<<endl;
cout<<"Estado: "<<((est()) ? "APROBADO" : "REPROBADO" )<<endl;
cout<<"----------------------------------"<<endl<<endl;
find=true;
}
leodat>>e[A].cod;
}
leodat.close();
system("pause");
cout<<"PULSE UNA TECLA PARA BORRAR PANTALLA(...)"<<endl;
system("cls");
if(!find){
cout<<"\t ** ERROR DATA Estudiante no encontrado **"<<endl;
}
}
else{
cout<<"\t**La libreria no existe ** "<< endl;
}
}
void modificar(){
system("cls");
cout<<"\t ** Modificar registro de estudiante ** "<<endl;
cout<<"Ingrese Codigo del estudiante: ";
cin>>e[A].cod;
if(buscar(e[A].cod)){
leodat.open("PROGRAMAPARCIAL.txt", ios::out|ios::in);
if (leodat.is_open()) {
editodat.open("PROGRAMAPARCIALAUX.txt", ios::app);
if (!editodat.fail()){
leodat>>aux.cod;
while (!leodat.eof()) {
leodat>>aux.nom>>aux.ape>>aux.mat>>aux.n1>>aux.n2>>aux.n3;
if(strcmp(aux.cod,e[A].cod) == 0){
cout<<"\t ** ESTOS SON LOS DATOS QUE DESEAS MODIFICAR? **"<<endl;
cout<<"Nombres del Estudiante: "<<aux.nom<<endl;
cout<<"Apellidos del Estudiante: "<<aux.ape<<endl;
cout<<"Materia y Codigo de Materia: "<<aux.mat<<endl;
cout<<"Primer Corte: "<<aux.n1<<endl;
cout<<"Segundo Corte: "<<aux.n2<<endl;
cout<<"Tercer Corte: "<<aux.n3<<endl;
cout<<endl<<"Por favor modifique los datos que desea, sino es el caso, ingrese los mismos datos correspondientemente(...)"<<endl;
cout<<"Ingrese Nuevos Nombres del Estudiante: ";cin>>aux.nom;
cout<<"Ingrese Nuevos Apellidos del Estudiante: ";cin>>aux.ape;
cout<<"Ingrese nueva materia y codigo de asignatura: ";cin>>aux.mat;
cout<<"Ingrese primer corte: ";cin>>aux.n1;
cout<<"Ingrese segundo corte: ";cin>>aux.n2;
cout<<"Ingrese tercer corte: ";cin>>aux.n3;
}
editodat<<aux.cod<<" "<<aux.nom<<" "<<aux.ape<<" "<<aux.mat<<" "<<aux.n1<<" "<<aux.n2<<" "<<aux.n3<<endl;
leodat>>aux.cod;
}
leodat.close();
editodat.close();
}else cout<<"\t ** ERROR AL CREAR ARCHIVO AUXILIAR **";
if(remove("PROGRAMAPARCIAL.txt")==0) {
if(rename("PROGRAMAPARCIALAUX.txt","PROGRAMAPARCIAL.txt")==0)
cout<<"\t ** EXITO AL MODIFICAR **"<<endl;
else
cout<<"\t ERROR DATA: ERROR AL MODIFICAR EL NOMBRE DEL ARCHIVO."<<endl;
}else
cout<<"\t ** ERROR DATA: AL ELIMIANR EL ARCHIVO"<<endl;
}
else cout<<"\t **La librería no existe ** " <<endl;
}
else{
cout<<"\t ** ERROR DATA Estudiante no encontrado **"<<endl;
}
system("pause");
cout<<"PULSE UNA TECLA PARA BORRAR PANTALLA(...)"<<endl;
system("cls");
}
void eliminar(){
system("cls");
cout<<"Eliminar registro de estudiante: "<<endl;
cout<<"Ingrese el codigo del estudiante: ";
cin>>e[A].cod;
if(buscar(e[A].cod)){
leodat.open("PROGRAMAPARCIAL.txt", ios::out|ios::in);
if (leodat.is_open()) {
editodat.open("PROGRAMAPARCIALAUX.txt", ios::app);
if (!editodat.fail()){
bool find;
leodat>>aux.cod;
while (!leodat.eof()) {
find=false;
leodat>>aux.nom>>aux.ape>>aux.mat>>aux.n1>>aux.n2>>aux.n3;
if(strcmp(aux.cod,e[A].cod) == 0)
find=true;
if(!find)
editodat<<aux.cod<<" "<<aux.nom<<" "<<aux.ape<<" "<<aux.mat<<" "<<aux.n1<<" "<<aux.n2<<" "<<aux.n3<<endl;
leodat>>aux.cod;
}
leodat.close();
editodat.close();
}else cout<<"\t ** ERROR AL CREAR ARCHIVO AUXILIAR **";;
if(remove("PROGRAMAPARCIAL.txt")==0) {
if(rename("auxiliar.txt","Fichero.txt")==0)// Renombramos el archivo
cout<<"\t ** EXITO AL ELIMINAR** "<<endl;
else
cout<<"\t ** ERROR AL RENOMBRAR EL ARCHIVO **"<<endl;
}else
cout<<"\t ** ERROR AL ELIMINAR EL ARCHIVO **"<<endl;
}
else cout << "\t ** EL FICHERO NO EXISTE /NO TIENE PERMISOS **" << endl;
}else{
cout<<"\t ** ERROR DATA: ESTUDIANTE NO ENCONTRADO **"<<endl;
}
system("pause");
cout<<"PULSE UNA TECLA PARA BORRAR PANTALLA(...)"<<endl;
system("cls");
}
float promedio(){
float prom, n1, n2, n3;
prom = (e[A].n1*0.3)+(e[A].n2*0.3)+(e[A].n3*0.4);
return(prom);
}
bool est(){
return promedio() >= 3.0;
}
main(){
int opc=0;
while (opc <= 6){
cout<<"\t\t ** MENU DE OPCIONES **: "<<endl;
cout<<"\t Ing. Roberto Fernandez - Programacion de COMPUTADORES 1"<<endl;
cout<<"1. Ingresar datos"<<endl;
cout<<"2. Consulta general "<<endl;
cout<<"3. Consulta individual "<<endl;
cout<<"4. Modificar "<<endl;
cout<<"5. Eliminar"<<endl;
cout<<"6. Salir \t"<<endl;
cout<<"Seleccione una opcion: ";
cin>>opc;
switch(opc){
case 1:ingresar();
break;
case 2:general();
break;
case 3:individual();
break;
case 4:modificar();
break;
case 5:eliminar();
break;
case 6:
break;
}
}
return 0;
}
Valora esta pregunta


0