#include "stdafx.h" #include "stdlib.h" #include #include "stdio.h" #include #include struct stRegistro { char valido; // Campo que indica si el registro es válido S->Válido, N->Inválido char nombre[34]; char rut[12]; int nota[4]; }; int Menu(); void Leer(struct stRegistro *reg); void Listar(long n, struct stRegistro *reg); long LeeNumero(); void Empaquetar(FILE **fa); int main() { struct stRegistro reg; FILE *fa; int opcion; long numero; fa = fopen("notas.dat", "r+b"); // Este modo permite leer y escribir if(!fa) fa = fopen("notas.dat", "w+b"); // si el fichero no existe, lo crea. do { opcion = Menu(); switch(opcion) { case '1': // Mostrar todo // Añadir registro Leer(®); // Insertar al final: fseek(fa, 0, SEEK_END); fwrite(®, sizeof(struct stRegistro), 1, fa); system("PAUSE"); break; case '2': // Mostrar todo rewind(fa); numero = 0; system("cls"); printf(" *** INFORME FINAL DE CALIFICACIONES ***\n\n"); printf("Reg. Rut Nombre Notas Promedio\n"); printf("-------------------------------------------------------------------\n"); while(fread(®, sizeof(struct stRegistro), 1, fa)) Listar(numero++, ®); system("PAUSE"); break; case '3': // Eliminar registro system("cls"); printf("Eliminar registro: "); numero = LeeNumero(); fseek(fa, numero*sizeof(struct stRegistro), SEEK_SET); fread(®, sizeof(struct stRegistro), 1, fa); reg.valido = 'N'; fseek(fa, numero*sizeof(struct stRegistro), SEEK_SET); fwrite(®, sizeof(struct stRegistro), 1, fa); break; case '4': // Eliminar marcados Empaquetar(&fa); break; } } while(opcion != '0'); fclose(fa); return 0; } // Muestra un menú con las opciones disponibles y captura una opción del usuario int Menu() { char resp[20]; do { system("cls"); printf(" ***MENU***\n\n"); printf("1.- CREAR ARCHIVO\n"); printf("2.- LISTAR DATOS\n"); printf("3- Eliminar registro\n"); printf("4- Eliminar registros marcados\n"); printf("0.- SALIR\n\n"); printf("SELECCIONE OPCION: "); fgets(resp, 20, stdin); } while(resp[0] < '0' && resp[0] > '4'); return resp[0]; } // Permite que el usuario introduzca un registro por pantalla void Leer(struct stRegistro *reg) { int i; char numero[6]; system("cls"); printf("Leer registro:\n\n"); reg->valido = 'S'; printf("Rut: "); fgets(reg->rut, 12, stdin); // la función fgets captura el retorno de línea, hay que eliminarlo: for(i = strlen(reg->rut)-1; i && reg->rut[i] < ' '; i--) reg->rut[i] = 0; printf("Nombre: "); fgets(reg->nombre, 34, stdin); // la función fgets captura el retorno de línea, hay que eliminarlo: for(i = strlen(reg->nombre)-1; i && reg->nombre[i] < ' '; i--) reg->nombre[i] = 0; for(i = 1; i < 4; i++) { printf("Nota[%1d]: ", i); fgets(numero, 6, stdin); reg->nota[i] = atoi(numero); } } // Muestra un registro por pantalla en forma de listado, // si no está marcado como borrado void Listar(long n, struct stRegistro *reg) { int i; if(reg->valido == 'S') { printf("[%ld] %-10s", n, reg->rut); printf(" %-25s",reg->nombre); for(i = 1; i < 4; i++) printf("%4d", reg->nota[i]); } printf(" \n"); } // Lee un número suministrado por el usuario long LeeNumero() { char numero[6]; fgets(numero, 6, stdin); return atoi(numero); } // Elimina los registros marcados como borrados void Empaquetar(FILE **fa) { FILE *ftemp; struct stRegistro reg; ftemp = fopen("notas.tmp", "wb"); rewind(*fa); while(fread(®, sizeof(struct stRegistro), 1, *fa)) if(reg.valido == 'S') fwrite(®, sizeof(struct stRegistro), 1, ftemp); fclose(ftemp); fclose(*fa); remove("notas.bak"); rename("notas.dat", "notas.bak"); rename("notas.tmp", "notas.dat"); *fa = fopen("notas.dat", "r+b"); }