Pascal/Turbo Pascal - Búsqueda en archivo de registro

 
Vista:

Búsqueda en archivo de registro

Publicado por Decals (11 intervenciones) el 03/11/2016 01:25:51
Hola ! Tengo una duda: ¿Cómo puedo hacer una búsqueda en un archivo de registros ? Supongan que tengo codigo, nombre, apellido, etc y quiero buscar por código... si el código existe que me muestre el registro que pertenece a ese codigo. Gracias
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Búsqueda en archivo de registro

Publicado por David (224 intervenciones) el 03/11/2016 19:33:40
Una forma sencilla de hacer es hacer una búsqueda lineal

1
2
3
4
5
6
7
8
9
10
{vamos a suponer el fichero ya creado}
writeln('Introduzca el código a buscar: ');
readLn(cod);
reset(fichero);
while not eof(fichero) do
begin
   read(fichero,registro);
  if registro.codigo = cod
   {presentamos el registro en pantalla}
end;

Otra forma de hacerlo, dependiendo del tipo de código utilizado podría ser apuntar directamente con seek

see(f, posicion)
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Búsqueda en archivo de registro

Publicado por ramon (2158 intervenciones) el 03/11/2016 23:12:54
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
{A ver si esto ayuda}
 
program busqueda;
  uses
    crt;
 
   const
      archivo = 'datosreg.dat';
   type
     losdatos = record
          codigo : word;
          nombre : string[30];
          apellido : string[40];
          telefono : word;
          end;
 
   var
     datos : losdatos;
     f : file of losdatos;
     cont : longint;
 
   procedure guardaregistro(rg : losdatos);
   begin
      assign(f,archivo);
  {$I-} reset(f); {$I+}
      if ioresult <> 0 then
      begin
          rewrite(f);
          seek(f,0);
          write(f,rg);
          close(f);
      end
   else
      begin
         seek(f,filesize(f));
         write(f,rg);
         close(f);
      end;
   end;
 
   procedure entradadatos;
   var
     dd : losdatos;
     ace : char;
   begin
      clrscr;
      writeln(' ##### Entrada De Datos ######');
      writeln;
      write('   Entre Codigo   : ');
      readln(dd.codigo);
      write('   Entre Nombre   : ');
      readln(dd.nombre);
      write('   Entre Apellido : ');
      readln(dd.apellido);
      write('   Entre Telefono : ');
      readln(dd.telefono);
      writeln;
      writeln('   Datos Correctos [S/N]');
    repeat
        ace := upcase(readkey);
    until ace in['S','N'];
    if ace = 'S' then
    begin
      guardaregistro(dd);
    end;
   end;
 
 
   procedure tomadatos(cod : word);
   var
     ok : boolean;
   begin
      assign(f,archivo);
  {$I-} reset(f); {$I+}
      if ioresult <> 0 then
      begin
         writeln('   Error de Archivo O No Existe Pulse Una Tecla');
         readkey;
      end
    else
       begin
          ok := false;
          for cont := 0 to filesize(f) - 1 do
          begin
             seek(f,cont);
             read(f,datos);
             if datos.codigo = cod then
             begin
                close(f);
                ok := true;
                break;
             end;
          end;
            if ok = true then
            begin
            writeln('   Los Datos Pedidos Son ');
            writeln;
            writeln('   El Codigo   : ',datos.codigo);
            writeln('   El Nombre   : ',datos.nombre);
            writeln('   El Apellido : ',datos.apellido);
            writeln('   El Telefono : ',datos.telefono);
            writeln;
            writeln('   Pulse Una Tecla');
            readkey;
           end
        else
           begin
              writeln('   El codigo No Existe Pulse Una Tecla');
              readkey;
           end;
       end;
   end;
 
   procedure menu;
   var
     sal : boolean;
     tec : char;
     codi : word;
     begin
        sal := false;
        repeat
            clrscr;
            writeln('   ***** Menu Jeneral *****');
            writeln;
            writeln('   [E] = Entrada Datos');
            writeln('   [B] = Busqueda Dato');
            writeln('   [S] = Salida');
            writeln;
            writeln('   Elija Opcion');
         repeat
            tec := upcase(readkey);
         until tec in['E','B','S'];
         clrscr;
     case tec of
  'E' : entradadatos;
  'B' : begin
          clrscr;
          writeln(' @@@@@ Busqueda Por Codigo @@@@@');
          writeln;
          write('  Entre Codigo : ');
          readln(codi);
          tomadatos(codi);
        end;
  'S' : sal := true;
     end;
     until sal = true;
   end;
 
   begin
      menu;
   end.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar