{separa arquivo em 2 arquivos}
program arquivos;
var f, arq1, arq2: text; {file of char}
numero, nota: integer;
begin
assign(f, `alunos');
assign(arq1, `aprovados');
assign(arq2, `reprovados');
reset(f); rewrite(arq1);rewrite(arq2);
{ opcao = reset(f, `alunos);}
while not eof(f) do begin
readln(f, numero, nota);
if nota >= 60
then writeln(arq1, numero, nota);
else writeln(arq2, numero, nota);
end;
close(arq1); close(arq2);
end.
caracter ASCII decimal
` ` 010 0000 32
`*' 010 1010 42
`0' 011 0000 48
`1' 011 0001 49
Funções:
{ conta letras de um texto }
program contaletras;
var c: char;
k: integer;
f: text;
begin
k := 0;
reset(f, `arquivo');
while not eof(f) do begin
read(f, c);
if ((c >= `A') and (c <= `Z')) or
((c >= `a') and (c <= `z'))
then k := k + 1;
end;
writeln(`Numero de letras = `, k);
end.
program contapalavras;
const branco = ` `;
var c: char;
k: integer;
f: text;
begin
reset(f, `arquivo');
k := 0;
while not eof(f) do begin
repeat
read(f, c);
until (c <> branco) or eof(f);
if not eof(f) then begin
k := k + 1;
repeat
read(f, c);
until (c = branco) or eof(f);
end;
end;
writeln(`Numero de palavras = `, k);
end.
AAAA
BBBB
CCCCé representado internamente como:
{ Conta linhas e caracteres de um texto}
program contalinhas;
var c: char;
linhas, caracteres: integer;
f: text;
begin
reset(f, `arquivo');
linhas := 0; caracteres := 0;
while not eof(f) do begin
if eoln(f) then begin
linhas := linhas + 1;
readln(f);
end
else begin
caracteres := caracteres + 1;
read(f, c);
end;
end;
writeln(`Linhas = `, linhas);
writeln(`Caracteres = `, caracteres);
end.