Type
Tarjeta=record
Nom:string[20];
Cognom:string[20];
Num_cass:string[30];
End;
Var
Fitxa = FILE OF tarjeta;
Operacions de lectura i escriptura en fitxers de dades
Operacions de lectura i escriptura en fitxers de text
program ex_fitxers;
uses crt;
type dades=record
nom:string[20];
alias:string[20];
end;
fitxer=file of dades;
procedure inicialitzar(var f1:fitxer);
begin
assign(f1,'c:\jugadors.txt');
rewrite(f1);
close(f1);
end;
procedure obrir(var f1:fitxer);
begin
assign(f1,'c:\jugadors.txt');
reset(f1);
end;
procedure tancar(var f1:fitxer);
begin
close(f1);
end;
procedure afegir(var f1:fitxer; reg1:dades);
begin
seek(f1, filesize(f1));
write('entra el nom:');readln(reg1.nom);
write('entra el alias:');readln(reg1.alias);
write(f1,reg1);
end;
procedure borrar(var f1:fitxer; reg1:dades;nom:string);
begin
while not eof(f1) do
begin
read(f1,reg1);
if reg1.nom<>nom then
{ no hi cap funci¢ que elimini un registre }
{ la eliminaci¢ del registre s'hauria de fer de forma l•gica }
{ ‚s a dir copiar els registre que no s'eliminen a un altre fitxer}
{ un fitxer auxiliar }
end;
end;
procedure llistat (var f1:fitxer; reg1:dades);
begin
Writeln('nom alias');
while not eof(f1) do
begin
read(f1,reg1);
writeln(reg1.nom:2,reg1.alias:20);
end;
end;
procedure menu(var ch:char);
begin
clrscr;
writeln('----- Men£ -----');
writeln;
writeln('1: crear fitxer');
writeln('2: afegir registre');
writeln('3: eliminar');
writeln('4: llistat');
writeln('5: Sortir');
write('entra la opci¢');
readln(ch);
end;
var f:fitxer;
reg:dades;
op:char;
nom:string[20];
begin
repeat
repeat
menu(op);
until op in['1'..'5'];
case op of
'1':inicialitzar(f);
'2':begin
obrir(f);
afegir(f,reg);
tancar(f);
end;
'3':begin
obrir(f);
write('entra nom a leiminar');
readln(nom);
borrar(f,reg,nom);
tancar(f);
end;
'4':begin
obrir(f);
llistat(f,reg);
writeln('prem una tecla per continuar');readkey;
tancar(f);
end;
end;
until op='5';
end.
Cap comentari:
Publica un comentari a l'entrada