25.06.2013 Views

Il Linguaggio Fortran 90/95

Il Linguaggio Fortran 90/95

Il Linguaggio Fortran 90/95

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

442 Operazioni di I/O su file<br />

CHARACTER(LEN=80) :: cdata<br />

INTEGER, PARAMETER :: in=20<br />

OPEN(UNIT=in,FILE=’accounts’,STATUS=’OLD’,IOSTAT=error)<br />

IF(error /= 0) THEN<br />

PRINT*, "Errore durante l’apertura del file"<br />

STOP<br />

END IF<br />

READ(UNIT=in,FMT=’(A)’) cdata<br />

! Stampa il file interno "cdata" per mostrare come appare<br />

PRINT*, cdata<br />

! Sostituisce tutti i caratteri "$" con uno spazio bianco<br />

DO k=1,80<br />

IF(cdata(k:k)==’$’)THEN<br />

cdata(k:k)=’ ’<br />

END IF<br />

END DO<br />

! Ora assegna i valori letti come reali agli elementi dell’array money<br />

READ(UNIT=cdata,FMT=’(10(1X,F4.2))’)money<br />

! Stampa l’array money per mostrare come ha lavorato<br />

PRINT*, money<br />

END PROGRAM internal_file<br />

Supponendo, ad esempio, che il file esterno accounts contenga la stringa:<br />

1.52$3.50$2.11$5.25$4.02$1.25$3.50$2.04$5.55$1.30<br />

l’output del programma sarebbe:<br />

1.520000 3.500000 2.110000 5.250000 4.020000<br />

1.250000 3.500000 2.040000 5.550000 1.300000

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!