Hace una busqueda en un archivo (seek)
pascal:
program buscar;
uses
crt;
type
Direc = record
Nombre : string[30];
Calle : string[25];
Ciudad : string[15];
Telefono : string[15]
end;
Archivo = file of Direc;
var
Dir : Archivo;
Elemento : Direc;
Respuesta : char;
Resultado : integer;
Posicion : integer;
NomBuscar : String[30];
Hallado : boolean;
BEGIN
clrscr;
Assign(Dir,'c:/Agenda2.dat');
{$I-}
Reset(Dir); {Abre el flujo de datos para posteriormente ser guardado}
Resultado:=IOresult;
{$I-}
if Resultado <> 0 then
Rewrite(Dir);
Hallado:=false;
Write('Escribe el nombre a buscar: ');
Readln(NomBuscar);
while not Eof(Dir) and not Hallado do
begin
Read(Dir,Elemento);
writeln(Elemento.nombre);
Hallado:=Elemento.Nombre = NomBuscar;
end;
if Hallado then
begin
posicion:=FilePos(Dir) - 1;
clrscr;
seek(Dir,posicion); {filesize: regresar el numero de registros}
with Elemento do
repeat
Write('Nombre :');
ReadLn (Nombre);
Write('Calle :');
ReadLn (Calle);
Write('Ciudad :');
ReadLn (ciudad);
Write('Telefono:');
ReadLn (Telefono);
Write (Dir,Elemento); {Escritura de un registro}
WriteLn('Desea otro elemento s/n');
respuesta := upcase(readkey)
until respuesta = 'N';
write ('Se encuentra en ' , posicion)
end
else
write('No se encontro el registro');
readkey;
Close(Dir)
END.
uses
crt;
type
Direc = record
Nombre : string[30];
Calle : string[25];
Ciudad : string[15];
Telefono : string[15]
end;
Archivo = file of Direc;
var
Dir : Archivo;
Elemento : Direc;
Respuesta : char;
Resultado : integer;
Posicion : integer;
NomBuscar : String[30];
Hallado : boolean;
BEGIN
clrscr;
Assign(Dir,'c:/Agenda2.dat');
{$I-}
Reset(Dir); {Abre el flujo de datos para posteriormente ser guardado}
Resultado:=IOresult;
{$I-}
if Resultado <> 0 then
Rewrite(Dir);
Hallado:=false;
Write('Escribe el nombre a buscar: ');
Readln(NomBuscar);
while not Eof(Dir) and not Hallado do
begin
Read(Dir,Elemento);
writeln(Elemento.nombre);
Hallado:=Elemento.Nombre = NomBuscar;
end;
if Hallado then
begin
posicion:=FilePos(Dir) - 1;
clrscr;
seek(Dir,posicion); {filesize: regresar el numero de registros}
with Elemento do
repeat
Write('Nombre :');
ReadLn (Nombre);
Write('Calle :');
ReadLn (Calle);
Write('Ciudad :');
ReadLn (ciudad);
Write('Telefono:');
ReadLn (Telefono);
Write (Dir,Elemento); {Escritura de un registro}
WriteLn('Desea otro elemento s/n');
respuesta := upcase(readkey)
until respuesta = 'N';
write ('Se encuentra en ' , posicion)
end
else
write('No se encontro el registro');
readkey;
Close(Dir)
END.