Fortran input/output

Here are some options for the open() and read() statements in Fortran that I keep looking up, as well as format descriptors for the read() and write() statements.

open():

  • action = ‘read’,’write’,’readwrite’
  • file = ‘file.dat’
  • form = ‘formatted’,’unformatted’
  • iostat = intvar intvar=0 if ok
  • position = ‘append’,’rewind’,’asis’
  • status = ‘old’,’new’,’replace’,’scratch’,’unknown’
  • unit = intvar

read():

  • unit:  The parameter unit is an integer that indicates the unit/port to read from
  • advance=’no’  Do not advance to the next line after reading
  • iostat=intvar  intvar: 0: ok, <0: end of file/end of record, >0: read error

read/write units:

The read/write unit or port is assigned with the ‘unit’ option in open(). Standard values (which don’t need to be opened or closed) are:

  • 0:  standard error output (stderr)
  • 5:  standard input (stdin); compiler dependent in principle, use * instead
  • 6:  standard output (stdout); compiler dependent in principle, use * instead
  • *:  standard in/out when reading/writing; compiler-independent

Format descriptors in read() and write():

  • I:  Integer: rIw or rIw.m
  • F:  Floating point: rFw.d
  • E:  Floating point (exponential) rEw.d (w>=d+7)
  • ES:  Floating point (scientific exponential): rESw.d (w>=d+7)
  • L:  Logical: rLw
  • A:  Character: rA or rAw
  • X:  Horizontal space: nX n: Number of spaces to skip
  • T:  Horizontal tab: Tc c: Column number
  • /:  Line break: /
  • ():  Repeat group: r(…)

Symbols used:

  • d:  Number of digits to the right of the decimal point (for floating-point data)
  • m:  Minimum number of digits to be displayed
    • with I: causes leading zeroes where appropriate
  • r:  Repeat count – the number of times to use a (group of) descriptor(s)
  • w:  Field width – the number of characters to use
    • set to 0 (zero) to automatically determine w (seems to be true for I, F, others? – >=F95)
  • Tab to a specific column:
    write(*,'(T32,A)') 'bla'
    • This would start the text in column 31, since the first column in the buffer is the “control character”
  • Line break in formatted output:   / (with or without comma):
    write(*,(/A,/,A/') 'bla1', 'bla2'

Simple template for opening and reading a file:

  integer, parameter :: n=100
  integer :: status
  real :: dat(n)

  open(unit=10,form='formatted',status='old',action='read',position='rewind',file='file.dat',iostat=status)
  if(status.ne.0) then
    write(0,'(A)')' Error opening file.dat, aborting...'
    stop
  end if

  do i=1,n
    read(10,'(F8.2)',iostat=status) dat(i)
    if(status.lt.0) exit
    if(status.gt.0) then
      write(0,'(A,I4,A)')' Error reading file.dat, line',i,' aborting...'
      stop
    end if
  end do

  close(10)
Advertisements
This entry was posted in All, Software and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s