/* BORRA registros de un fichero secuencial, utilizando un fichero auxiliar para copiar los registros no eliminados */ #include #include #include typedef struct { char nombre[30]; int edad; int curso; int nota; } TIPOALUMNO; void MostrarCabeceras (void) { gotoxy(1, 8); puts("---------------------------------------------"); puts(" DATOS ALUMNO "); puts("---------------------------------------------"); puts(" NOMBRE :"); puts(" EDAD :"); puts(" CURSO :"); puts(" NOTA :"); puts("---------------------------------------------"); } void MostrarCampos (TIPOALUMNO reg) { /* Borro los textos anteriores */ gotoxy(15, 11); puts(" "); gotoxy(15, 12); puts(" "); gotoxy(15, 13); puts(" "); gotoxy(15, 14); puts(" "); /* Muestro los nuevos valores */ gotoxy(15, 11); puts(reg.nombre); gotoxy(15, 12); printf("%d",reg.edad); gotoxy(15, 13); printf("%d",reg.curso); gotoxy(15, 14); printf("%d",reg.nota); } main () { FILE *fent,*fsal; TIPOALUMNO ralumno; char borrar; int nregborrados = 0; // Contador de registros eliminados fent = fopen("ALUMNOS.DAT","rb"); if ( fent == NULL ) { perror("ALUMNOS.DAT"); return 1; } // Creo un fichero temporal fsal = fopen("ALUMNOS.TMP","wb"); if ( fsal == NULL ) { perror("ALUMNOS.TMP"); return 2; } clrscr(); // Borro la pantalla MostrarCabeceras(); fread(&ralumno,sizeof(TIPOALUMNO),1,fent ); while ( !feof(fent) ) { MostrarCampos(ralumno); gotoxy(20,16); printf( "Borrar (s/n):"); borrar = getch(); if ( toupper(borrar) == 'S') { nregborrados++; } else { // Si no hay que borrarlo lo copio al auxiliar fwrite(&ralumno,sizeof(TIPOALUMNO),1,fsal ); } fread(&ralumno,sizeof(TIPOALUMNO),1,fent ); } // Cierro los ficheros fclose(fent); fclose(fsal); // Borro el fichero original remove("ALUMNOS.DAT"); // Cambio de nombre el fichero auxiliar rename("ALUMNOS.TMP","ALUMNOS.DAT"); printf("\n\t Registros eliminados : %d\n", nregborrados); getch(); return 0; }