1. // Lee el registro desde el fichero de datos con el teléfono dado
  2. void Leer(FILE *fa, stRegistro &reg, char *telefono)
  3. {
  4. FILE *fi;
  5. stIndice ind;
  6. long inf, sup, n, nRegs;
  7. fi = fopen("indices.ind", "rb");
  8. fseek(fi, 0, SEEK_END);
  9. nRegs = ftell(fi)/sizeof(stIndice);
  10. // Búsqueda binaria:
  11. inf = 0;
  12. sup = nRegs-1;
  13. do {
  14. n = inf+(sup-inf)/2;
  15. fseek(fi, n*sizeof(stIndice), SEEK_SET);
  16. fread(&ind, sizeof(stIndice), 1, fi);
  17. if(strcmp(ind.telefono, telefono) < 0) inf = n+1;
  18. else sup = n-1;
  19. } while(inf <= sup && strcmp(ind.telefono, telefono));
  20. // Si se encontró el teléfono, lee el registro, si no muestra mensaje.
  21. if(!strcmp(ind.telefono, telefono)) {
  22. fseek(fa, ind.indice*sizeof(stRegistro), SEEK_SET);
  23. fread(&reg, sizeof(stRegistro), 1, fa);
  24. }
  25. else {
  26. reg.valido = 'N';
  27. printf("Registro no encontrado\n");
  28. }
  29. fclose(fi);
  30. }