quinta-feira, 29 de novembro de 2012

Exemplos de ficheiros:

Exemplo de um ficheiro do tipo definido pelo programador:


Program estacionamento;
type popo=record
marca:string;
matricula:string;
tempo:integer;
tipo:string;
end;
var fich: file of popo;
i,n,cont,maior,op:integer;
mat,matri:string;
        parque:popo;
procedure criar_ficheiro;
begin
assign(fich,'estacionar');
rewrite(fich);
end;
procedure dados;
begin
reset(fich);
writeln('Quantos ve¡culos?');
readln(n);
seek(fich, filesize(fich));
for i:=1 to n do
begin
writeln('Introduza a marca do ve¡culo');
Readln(parque.marca);
Writeln('Introduza a matr¡cula do ve¡culo');
readln(parque.matricula);
writeln('Tempo de estacionamento');
readln(parque.tempo);
writeln('Introduza o tipo de ve¡culo - ligeiro, pesado, moto');
readln(parque.tipo);
                        Writeln('*************************************');
                writeln('*************************************');
write(fich,parque);
end;
close(fich);
end;
procedure listar;
begin
reset(fich);
cont:=0;
        If filesize(fich)=0 then
        Write('NÆo h  registos no ficheiro')
        Else
        Begin
while not eof (fich) do
begin
read(fich,parque);
Writeln('Matricula: ',parque.matricula);
Writeln('Tempo de estacionamento: ', parque.tempo);
Writeln('Tipo de veiculo: ', parque.tipo);
writeln('****************************************');
                        writeln('****************************************');
cont:=cont+parque.tempo;
end;
writeln('Tempo m‚dio: ', cont/filesize(fich));
        seek(fich,0);
maior:=parque.tempo;
mat:=parque.matricula;
while not eof (fich) do
begin
if maior<parque.tempo then
begin
maior:=parque.tempo;
mat:=parque.matricula;
end;
end;
writeln('Autom¢vel com mais tempo:',mat,'Tempo= ',maior);
        end;
close(fich);
end;
procedure alterar;
begin
writeln('Introduza a matr¡cula a alterar dados');
read(matri);
reset(fich);
seek(fich,0);
while not eof (fich) do
begin
if parque.matricula=matri then
begin
Writeln('MARCA');
readln(parque.marca);
writeln('Matr¡cula');
readln(parque.matricula);
writeln('Tempo estacionado');
readln(parque.tempo);
write(fich,parque);
end
else
writeln('Matr¡cula nÆo encontrada');
end;
close(fich);
end;
procedure relatorio;
begin
reset(fich);

while not eof (fich) do
        begin
        read(fich, parque);
if parque.tipo='moto' then
             begin
Writeln('Matr¡cula: ', parque.matricula);
writeln('Tempo: ', parque.tempo);
writeln('Valor pago: ', (0.04*parque.tempo)*0.2);
writeln('*********************************************');
                              writeln('*********************************************');
             end;
if parque.tipo='ligeiro' then
             begin
       Writeln('Matrícula: ', parque.matricula);
writeln('Tempo: ', parque.tempo);
writeln('Valor pago: ', (0.08*parque.tempo)*0.2);
writeln('*********************************************');
                              writeln('*********************************************');
             end;
if parque.tipo='pesado' then
             begin
               Writeln('Matrícula: ', parque.matricula);
writeln('Tempo: ', parque.tempo);
writeln('Valor pago: ', (0.1*parque.tempo)*0.2);
writeln('*********************************************');
                              writeln('*********************************************');
             end;
end;
        close(fich);
end;
procedure apagar;
begin
rewrite(fich);
end;
 Begin
    repeat
writeln('Escolha uma das opções');
    Writeln('*1-Criar ficheiro');
    Writeln('*2-Introduzir registos');
    writeln('*3-Apresentar dados');
    writeln('*4-Alterar dados');
    writeln('*5-Relat¢rio de contas');
    writeln('*6-Limpar dados');
    writeln('*7-Sair');
    readln(op);
    case op of
    1:criar_ficheiro;
    2:dados;
    3:listar;
    4:alterar;
    5:relatorio;
    6:apagar;
    end;
until(op=7);
 End.

Exemplo de um ficheiro do tipo texto:


Program Ficheiro_texto_1;
var F1, F2: Text;
        linha, nome:string;

begin
        assign(F1,'TESTE1.txt');
        rewrite(F1);
        write(F1,'ISTO  UM TESTE COM');
        write(F1,'FICHEIRO DE TEXTO');
        write(F1,'ESCREVA MAIS...');
        CLOSE(F1);
        Reset(F1);
        While not Eof (f1) do
         begin
                readln (f1,linha);
                writeln(linha);
        end;
        close(f1);
        writeln('Escvreva um nome de pessoa');
        readln(nome);
        append(f1);
        write(f1,nome);
        close(f1);
        reset(f1);
        while not Eof(f1)do
        begin
        readln(f1,linha,nome);
        Writeln(nome);
        Writeln(linha);
        end;
        close(f1);
        assign (F2,'Teste2.txt');
        rewrite(f2);
        while not eof (f1)do
                begin
                        readln(F1,linha);
                        writeln(F2,linha);
                end;
         while not eof (f2)do
                begin
                        readln(F2,linha);
                        write(linha);
                end;

 end.


Sem comentários:

Enviar um comentário