elfsummer
elfsummer

Reputation: 167

how to read "enter" key in fortran

The following fortran 90 program can read in a text file and then print it out on the screen. It has an added feature that when the output covers the whole screen it will pause and wait for the user to press enter key to continue. Currently I use the "PAUSE" to implement this feature. But I would like to know the direct way of reading in the enter key. Please do offer your wisdom. I appreciate it!

program ex0905
  implicit none
  character(len=79)  :: filename
  character(len=79)  :: buffer
  integer, parameter :: fileid = 10
  integer :: status = 0,count=0
  logical alive
  character(len=1) :: c

  write(*,*) "Filename:"
  read (*,"(A79)") filename
  inquire( file=filename, exist=alive)

  if ( alive ) then
     open(unit=fileid, file=filename, &
         access="sequential", status="old")
  do while(.true.)
     read(unit=fileid, fmt="(A79)", iostat=status ) buffer
     if ( status/=0 ) exit
     !write(*,"(A79)") buffer
     count = count+1
     if (count<24) then
        write(*,"(A79)") buffer
     else
       !write(*,*) "Please type Enter to continue: "
       pause
       count=0
       !read(*,"(A1)") c
       !if (c==char(13)) then
       !   write(*,"(A79)") buffer
       !else
       !   write(*,*) "This is not the 'Enter' key!!"
       !   exit
       !end if
     end if

   end do
  else
    write(*,*) TRIM(filename)," doesn't exist."
  end if

 stop  
end

Upvotes: 4

Views: 6646

Answers (1)

sigma
sigma

Reputation: 2963

If you have an item in the input list for read, as you do here with the character c, execution will not proceed until you do provide an actual item to standard input.

You can instead achieve what you want by having a read(*,*) statement without input. This will wait for just the key press, discarding any input.

Upvotes: 5

Related Questions