- // Lee el registro desde el fichero de datos con el teléfono dado
- void Leer(FILE *fa, stRegistro ®, char *telefono)
- {
- FILE *fi;
- stIndice ind;
- long inf, sup, n, nRegs;
- fi = fopen("indices.ind", "rb");
- fseek(fi, 0, SEEK_END);
- nRegs = ftell(fi)/sizeof(stIndice);
- // Búsqueda binaria:
- inf = 0;
- sup = nRegs-1;
- do {
- n = inf+(sup-inf)/2;
- fseek(fi, n*sizeof(stIndice), SEEK_SET);
- fread(&ind, sizeof(stIndice), 1, fi);
- if(strcmp(ind.telefono, telefono) < 0) inf = n+1;
- else sup = n-1;
- } while(inf <= sup && strcmp(ind.telefono, telefono));
- // Si se encontró el teléfono, lee el registro, si no muestra mensaje.
- if(!strcmp(ind.telefono, telefono)) {
- fseek(fa, ind.indice*sizeof(stRegistro), SEEK_SET);
- fread(®, sizeof(stRegistro), 1, fa);
- }
- else {
- reg.valido = 'N';
- printf("Registro no encontrado\n");
- }
- fclose(fi);
- }