Input/Output in Fortran

Formatted Input/Output

We often want aligned output, which can be achieved with a format statement

 write(*, fmt=”(i2,2x,f5.1,1x,a4)”) i,temp,name

Similarly, we can write with a separate, numbered format statement

 write(*,10) i, temp, name
10 format (i2,2x,f5.1,1x,a4)

Unformatted I/O

Gives the internal representation. Formatted output has several drawbacks:

  • Extra work with formatting

  • rounding

  • In general, formatted output takes up more space

Unformatted I/O preserves the internal structure.

Example of writing to a file:

 open(unit=10, file="unformatted.dat",& action="write", form="unformatted")
. . .
do i = 1, imax
  write(10, err=100) a(i)
  end do
100 continue
close(10)

Unit numbers

Files are addressed by the numbers of open units. Unit numbers are positive and can be used as integers in the program. Usually unit 5 represents input and unit 6 output.

Vhod/Izhod v Fortranu

Formatiran Vhod/Izhod (Input/Output)

Velokokrat želimo poravnan izpis, kar lahko naredimo s formatnim stavkom

 write(*, fmt=”(i2,2x,f5.1,1x,a4)”) i,temp,name

Podobni lahko pišemo z ločenim formatnim stavkom, ki ga oštevilčimo

 write(*,10) i, temp, name
10 format (i2,2x,f5.1,1x,a4)

Neformatiran I/O

Podaja interno presdtavitev. Formatiran izpis ima več slabosti:

  • Dodatno delo s formatiranjem

  • zaokroževanje

  • Na splošno pa formatiran izpis vzame več prostora

Neformatiran I/O omogoča ohranitev interne strukture.

Primer pisanja v datoteko:

 open(unit=10, file="unformatted.dat",& action="write", form="unformatted")
. . .
do i = 1, imax
  write(10, err=100) a(i)
  end do
100 continue
close(10)

Številke enot

Datoteke se naslavljajo s številkami odprtih enot. Številke enot so pozitivne in se lahko uporabljajo kot cela števila v programu.Običajo enota 5 predstavlja vhod, enota 6 pa izhod.