! 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 9.8. Replacing the last part of an existing file.
  program J08
    implicit none
    integer, parameter :: S_Len = 20
    character (len = S_Len) :: Key, String
    integer :: EoF
! start program J08
    read (unit = *, fmt = *) Key
    open (unit = 2, file = "fleeting.txt", status = "old", action = "readwrite", position = "rewind")
    do
      read (unit = 2, fmt = "( A )", iostat = EoF) String
      write (unit = *, fmt = *) String
      if ((String == Key) .or. (EoF < 0)) then
        write (unit = *, fmt = *) " Ending initial search. EoF:", EoF
        exit
      end if
    end do
    backspace ( unit = 2 )
    write (unit = *, fmt = *) " Please enter some strings to insert; end with ''."
    do
      read (unit = *, fmt = *) String
      if (String == " ") then
        exit
      end if
      write (unit = 2, fmt = "( A )" ) String
    end do
    stop
  end program J08
