      program backspace
      implicit none
      character*80 line
      integer i,ioerr1,ioerr2
c
c   Demonstrate some IO statements
c
c   John Mahaffy 11/15/96
c
c   Open a temporary file that will vanish when the program terminates
c
      open (12,file='io.out')
      do i=1,9
         write(12,2000) i
      enddo
 2000 format ('Line', i3)
c
c    Reposition to the beginning of the file
c
      rewind (12)
      do i=1,6
         read(12,2001) line
         write(6,2001)line
      enddo
c
c    check the output to see what happens here
c
      backspace(12)
      write(6,2001) '  After 1 backspace we find the line: '
      read(12,2001) line
      write(6,2001) line
c
c    March backwards through the file with:
c
      write(6,2001)' March backwards with Double Backspace'
      do i=1,10
         backspace(12,err=200, iostat=ioerr1 )
         backspace(12,err=200, iostat=ioerr2 )
         if (ioerr1.ne.0.or.ioerr2.ne.0) then
            print *, 'Error in Backup: Beginning of file'
            stop
         endif
         read(12,2001) line
         write(6,2001) line
      enddo
      print *, 'Backup Stalled at the beginning of file'
      print *, 'without an error flag or error branch'
      stop
  200 write(6,2001) 'Reached the beginning of the File'
 2001 format(a)
      end
