Example F Program--Character Count

! Copyright (c) 1994, 1996 Unicomp, Inc.
!
! Developed at Unicomp, Inc.
!
! Permission to use, copy, modify, and distribute this
! software is freely granted, provided that this notice 
! is preserved.

program char_count

   ! These two values are processor dependent.
   integer, parameter :: end_of_file = -1
   integer, parameter :: end_of_record = -2

   character (len = 1) :: c
   integer :: kount, ios

   kount = 0
   do
      read (unit=*, fmt="(a)", advance = "no", iostat = ios) c
      if (ios == end_of_record) then
         cycle
      else if (ios == end_of_file) then
         exit
      else
         kount = kount + 1
      end if
   end do

   print *, "The number of characters in the file is", &
            kount

end program char_count

Back to F Example Codes Page

Back to F Homepage


Questions or Comments?