! Program Example from "Essential Fortran 90 & 95" by Loren P. Meissner
! Copyright 1996. Copying for sale requires permission from the author.
! Otherwise, distribution is permitted if these three lines are included.

! Example 4.22. Exit when a negative data item is found
! or when the end of file is encountered.
  program E22
    implicit none
    integer :: Record_Number, EoF
    real :: Next_item
! start program E22
    open (unit = 2, file = "zahlen.txt", status = "old", action = "read", position = "rewind")
    Record_Number = 1
    do
      read (unit = 2, fmt = *, iostat = EoF) Next_item
      if (EoF < 0) then
        write (unit = *, fmt = *) " No negative items in file. "
        exit
      else if (Next_item < 0.0) then
        write (unit = *, fmt = *) " Negative item found at record number: ", Record_Number
        exit
      else
        Record_Number = Record_Number + 1
      end if
    end do
    stop
  end program E22
