! 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 3.8. (File input.) Count large, medium, and small cats.
  program D08
    implicit none
    integer, parameter :: LIMIT = 24
    integer :: Hi_Count, Mid_Count, Low_Count, Loop
    real, parameter :: MID_BOUNDARY = 2.7  ! Small vs. medium cats
    real, parameter :: HI_BOUNDARY = 4.3  ! Medium vs. large cats
    real :: Next_Weight
! start program D08
    Hi_Count = 0
    Mid_Count = 0
    Low_Count = 0
    open (unit = 1, file = "db.txt", status = "old", action = "read", position = "rewind")
    do Loop = 1, LIMIT
      read (unit = 1, fmt = *) Next_Weight
      if (Next_Weight > HI_BOUNDARY) then
        Hi_Count = Hi_Count + 1
      else
        if (Next_Weight > MID_BOUNDARY) then
          Mid_Count = Mid_Count + 1
        else
          Low_Count = Low_Count + 1
        end if
      end if
    end do
    write (unit = *, fmt = *) Hi_Count, Mid_Count, Low_Count
    stop
  end program D08
