Autore: Fabio Di Matteo
Ultima revisione: 06/05/2007
Una unit di freepascal con le principali funzioni per scrivere e leggere file di testo.
unit filetesto; interface uses sysutils; type fileditesto = text; {oppure "..= file of char"} albero = ^nodo;{alla fine non usato piu'} nodo = record parola:string; figliosx:albero; figliodx:albero; end; procedure Aggiungi(testo :string); procedure Leggi(); procedure Ordina(); procedure Conta(parola :string); implementation procedure Aggiungi(testo :string); var a : fileditesto; begin assign(a,'testo.dat'); if fileExists('testo.dat') then begin reset(a); append(a); writeln(a,testo); close(a); end else begin rewrite(a); append(a); writeln(a,testo); close(a); end; end; { Createsto } procedure Leggi(); var a : fileditesto; t : string; begin assign(a,'testo.dat'); if fileExists('testo.dat') then begin reset(a); while not eof(a) do begin readln(a,t); writeln(t); end; close(a); end else begin writeln('Errore: file non presente !'); end; end; { Leggi } procedure Ordina(); var vet : array of string; c,l,i,j : integer; testo : string; a : fileditesto; begin assign(a,'testo.dat'); c:=0; l:=0; reset(a); while not eof(a) do {per vedere quante righe ha} begin readln(a,testo); c:=c+1; end; close(a); l:=c; setlength(vet,l); c:=0; reset(a); while not eof(a) do{travaso da file a vettore} begin readln(a,testo); vet[c]:=testo; c:=c+1; end; close(a); for i:=0 to l-1 do{algoritmo di ordinamneto} begin for j:=0 to l-1 do begin if vet[i]<vet[j] then begin testo:=vet[j]; vet[j]:=vet[i]; vet[i]:=testo; end; end; end; writeln('Contenuto del file in ordine alfabetico:'); writeln(''); for i:=0 to l-1 do writeln(vet[i]);{stampa a video} end; { Ordina } procedure Conta(parola :string); var a : fileditesto; c : integer; testo : string; begin assign(a,'testo.dat'); reset(a); c:=0; while not eof(a) do begin readln(a,testo); if testo=parola then c:=c+1; end; close(a); writeln('La parola che hai cercato e''presente nel testo ',c,' volte.'); end; { Conta } end.
Il nostro wiki installa solamente cookie tecnici necessari al funzionamento della piattaforma "Dokuwiki". Niente analitics, statistiche, tracciamenti o altro.