首页 > 解决方案 > 比较 Fortran 输出中的 $ 编辑描述符和 Advance="no"

问题描述

我知道 $ 编辑描述符不在标准中,advance="no"建议使用,但是这个小例子表明它们有不同的行为(使用 ifort 但不使用 gfortran),我不明白为什么(它不会打扰我不止于此,但如果有人有我感兴趣的解释!)。

program p
  use sleep_mod
  implicit none
  integer           :: i
  character(len=80) :: mymsg

  mymsg = 'H e l l o   w o r l d !  W e l c o m e - B i e n v e n u e - W i l l k o m m e n'
!
!- Write the characters of "mymsg" on the same line with a time delay between them:
!
  write(*,*)

  do i = 1, len_trim(mymsg)
     call usleep (onesec/4)
     write(*,'(a1,$)') mymsg(i:i)             ! ok w/ gfortran, ok w/ ifort
    ! write(*,'(a1)',advance='no') mymsg(i:i) ! ok w/ gfortran, KO w/ ifort
  end do

  write(*,'(/)')

end program p

sleep_mod 模块如下:

module sleep_mod
  use iso_c_binding

  integer(c_int32_t), parameter :: onesec = 100000_c_int32_t

  interface ! found in http://computer-programming-forum.com (by Tobias Burnu)
     subroutine usleep (useconds) bind(C)
       use iso_c_binding
       implicit none
       integer(c_int32_t), value :: useconds
     end subroutine
  end interface

end module sleep_mod

使用 $ 版本,无论编译器是什么(ifort 或 gfortran),结果都符合预期:每个字符打印之间的时间延迟。advance='no'使用 gfortran 而不是使用 ifort 编译的版本获得了相同的结果。

标签: fortrangfortranintel-fortran

解决方案


推荐阅读