如何将输出写入 Fortran 中的字符串?

发布于 2024-11-03 09:31:49 字数 1512 浏览 0 评论 0原文

我需要将格式化输出写入字符串 DTSTR。它过去可以在layhe fortran下工作,但不能在gfortran

   INTEGER*2    MON,DAY,YEAR,HR,MINUTE,SEC,HUND
   CHARACTER    DY*2
   CHARACTER    DTSTR*24
   COMMON   /RD/ DTSTR
   ...
   ...
   WRITE(DTSTR,10)MON,DAY,YEAR,HR,MINUTE,DY,' ]'
  10 FORMAT('[ ',I2,'-',I2.2,'-',I4,2X,I2,':',I2.2,1X,2A2)

下工作,它只是一个空行。如果我使用正确的输出。但我想存储这个字符串。是否可以使用 gnu fortran 来做到这一点?

   WRITE(*,10)MON,DAY,YEAR,HR,MINUTE,DY,' ]'

更新

我正在尝试编译以下文件。我认为问题可能出在 COMMON 上。

      PROGRAM HELO

      CALL DOTIME

      WRITE(*,5700)DTSTR

 5700 FORMAT(24X,A24/)

      END


      SUBROUTINE DOTIME
C
      IMPLICIT NONE
      INTEGER*2    MON,DAY,YEAR,HR,MINUTE,SEC,HUND
      CHARACTER    DY*2
      CHARACTER    DTSTR*24
      COMMON   /RD/ DTSTR

      integer values(8)
      call date_and_time(VALUES=values)

        YEAR   = values(1)
        MON    = values(2)
        DAY    = values(3)
        HR     = values(5)
        MINUTE = values(6)
        SEC    = values(7)
        HUND   = values(8)
C     =================================================
C
C     Incompitable function => CALL GETDAT(YEAR,MON,DAY)
C     Incompitable function => GETTIM(HR,MINUTE,SEC,HUND)
      IF(HR .GE. 12)THEN
           IF(HR .NE. 12)HR=HR-12
           DY='PM'
      ELSE
           DY='AM'
      ENDIF
      WRITE(DTSTR,10)MON,DAY,YEAR,HR,MINUTE,DY,' ]'
   10 FORMAT('[ ',I2,'-',I2.2,'-',I4,2X,I2,':',I2.2,1X,2A2)
      RETURN
      END

I need to write a formated output to a string DTSTR. It use to work under layhe fortran but not gfortran

   INTEGER*2    MON,DAY,YEAR,HR,MINUTE,SEC,HUND
   CHARACTER    DY*2
   CHARACTER    DTSTR*24
   COMMON   /RD/ DTSTR
   ...
   ...
   WRITE(DTSTR,10)MON,DAY,YEAR,HR,MINUTE,DY,' ]'
  10 FORMAT('[ ',I2,'-',I2.2,'-',I4,2X,I2,':',I2.2,1X,2A2)

it empty just a empty line. If i use following it correctly output. But i want to store this string. Is it possible to do that with gnu fortran.

   WRITE(*,10)MON,DAY,YEAR,HR,MINUTE,DY,' ]'

update

I am trying to compile following file. I think the problem might be with the COMMON.

      PROGRAM HELO

      CALL DOTIME

      WRITE(*,5700)DTSTR

 5700 FORMAT(24X,A24/)

      END


      SUBROUTINE DOTIME
C
      IMPLICIT NONE
      INTEGER*2    MON,DAY,YEAR,HR,MINUTE,SEC,HUND
      CHARACTER    DY*2
      CHARACTER    DTSTR*24
      COMMON   /RD/ DTSTR

      integer values(8)
      call date_and_time(VALUES=values)

        YEAR   = values(1)
        MON    = values(2)
        DAY    = values(3)
        HR     = values(5)
        MINUTE = values(6)
        SEC    = values(7)
        HUND   = values(8)
C     =================================================
C
C     Incompitable function => CALL GETDAT(YEAR,MON,DAY)
C     Incompitable function => GETTIM(HR,MINUTE,SEC,HUND)
      IF(HR .GE. 12)THEN
           IF(HR .NE. 12)HR=HR-12
           DY='PM'
      ELSE
           DY='AM'
      ENDIF
      WRITE(DTSTR,10)MON,DAY,YEAR,HR,MINUTE,DY,' ]'
   10 FORMAT('[ ',I2,'-',I2.2,'-',I4,2X,I2,':',I2.2,1X,2A2)
      RETURN
      END

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅人 2024-11-10 09:31:49

唔?它对我来说效果很好:

program testwrite
    implicit none
    INTEGER :: MON,DAY,YEAR,HR,MINUTE,SEC,HUND
    CHARACTER(LEN=2)  :: DY
    CHARACTER(LEN=24) :: DTSTR

    MON = 4
    DAY = 27
    YEAR= 2010
    HR  = 13
    MINUTE = 27
    SEC  = 0
    HUND = 0
    DY   ='WE'

    WRITE(DTSTR,10)MON,DAY,YEAR,HR,MINUTE,DY,' ]'
 10 FORMAT('[ ',I2,'-',I2.2,'-',I4,2X,I2,':',I2.2,1X,2A2)

    print *,'<',trim(DTSTR),'>'

end program testwrite

正如人们所期望的那样

 <[  4-27-2010  13:27 WE ]>

。可以与我使用过的几个版本的 gfortran 配合使用。

更新:是的,问题出在您的公共块中。公共块未在主程序中声明。但实际上,将字符串作为参数传递是更简单、更好的做法:

      PROGRAM HELO
      IMPLICIT NONE
      CHARACTER(LEN=24) :: DTSTR

      CALL DOTIME(DTSTR)

      WRITE(*,5700)DTSTR

 5700 FORMAT(24X,A24/)

      END


      SUBROUTINE DOTIME(DTSTR)
C
      IMPLICIT NONE
      INTEGER*2    MON,DAY,YEAR,HR,MINUTE,SEC,HUND
      CHARACTER    DY*2
      CHARACTER(LEN=24), INTENT(OUT) ::   DTSTR

Hmm? It works just fine for me:

program testwrite
    implicit none
    INTEGER :: MON,DAY,YEAR,HR,MINUTE,SEC,HUND
    CHARACTER(LEN=2)  :: DY
    CHARACTER(LEN=24) :: DTSTR

    MON = 4
    DAY = 27
    YEAR= 2010
    HR  = 13
    MINUTE = 27
    SEC  = 0
    HUND = 0
    DY   ='WE'

    WRITE(DTSTR,10)MON,DAY,YEAR,HR,MINUTE,DY,' ]'
 10 FORMAT('[ ',I2,'-',I2.2,'-',I4,2X,I2,':',I2.2,1X,2A2)

    print *,'<',trim(DTSTR),'>'

end program testwrite

gives

 <[  4-27-2010  13:27 WE ]>

just as one would expect. Works with several versions of gfortran I have kicking around.

Update: Yes, the problem is in your common block. The common block isn't declared in the main program. But really, it's much simpler and much, much better practice just to pass the string as an argument:

      PROGRAM HELO
      IMPLICIT NONE
      CHARACTER(LEN=24) :: DTSTR

      CALL DOTIME(DTSTR)

      WRITE(*,5700)DTSTR

 5700 FORMAT(24X,A24/)

      END


      SUBROUTINE DOTIME(DTSTR)
C
      IMPLICIT NONE
      INTEGER*2    MON,DAY,YEAR,HR,MINUTE,SEC,HUND
      CHARACTER    DY*2
      CHARACTER(LEN=24), INTENT(OUT) ::   DTSTR
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文