【问题标题】:how to read "enter" key in fortran如何在fortran中读取“enter”键
【发布时间】:2014-06-26 20:10:22
【问题描述】:

下面的 fortran 90 程序可以读入一个文本文件,然后在屏幕上打印出来。它有一个附加功能,当输出覆盖整个屏幕时,它将暂停并等待用户按回车键继续。目前我使用“PAUSE”来实现这个功能。但我想知道在回车键中的直接阅读方式。请提供你的智慧。我很感激!

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

【问题讨论】:

    标签: input fortran


    【解决方案1】:

    如果您在 read 的输入列表中有一个项目,就像您在此处使用字符 c 所做的那样,则在您向标准输入提供实际项目之前不会继续执行。

    您可以通过在没有输入的情况下使用read(*,*) 语句来实现您想要的。这将只等待按键,丢弃任何输入。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      • 2010-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多