【问题标题】:strange character in Fortran write outputFortran 写入输出中的奇怪字符
【发布时间】:2016-01-23 10:11:38
【问题描述】:

我想给一些子程序计时。这是我用来写名称和执行时间的模板:

SUBROUTINE get_sigma_vrelp

  ...declarations...

  real(8) :: starttime, endtime
  CHARACTER (LEN = 200) timebuf
  starttime = MPI_Wtime()

  ...do stuff...

  endtime = MPI_Wtime()
  write (timebuf, '(30a,e20.10e3)') 'get_sigma_vrelp',endtime-starttime
  call pout(timebuf)
END SUBROUTINE get_sigma_vrelp

这是一个示例输出:

(thread  4):get_sigma_vrelp �>  

为什么 endtime-starttime 打印的是一个奇怪的字符而不是一个数值?顺便说一句,pout() 只是以线程安全的方式将缓冲区写入特定于进程的文件。它应该与问题无关,但如果这里没有其他会导致错误输出的内容,那么我可以发布它的正文。

【问题讨论】:

    标签: io formatting fortran mpi


    【解决方案1】:

    你搞错了!该行应为

    write (timebuf, '(a30,e20.10e3)') 'get_sigma_vrelp',endtime-starttime
    

    这样,您期望一个长度为 30 个字符的字符串 (a30) 而不是 30 个任意长度的字符串 (30a)。 write 语句接收的不是第一个字符串之后的字符,而是浮点数的相应字节。因此垃圾。

    您的字符文字只有 15 个字符长,因此您可以将这一行写成

    write (timebuf, '(a15,e20.10e3)') 'get_sigma_vrelp',endtime-starttime
    

    或者让编译器自己决定长度:

    write (timebuf, '(a,e20.10e3)') 'get_sigma_vrelp',endtime-starttime
    

    【讨论】:

    • 在你的第三个例子中,a15 应该是A吗?
    • 不,a 在那里很好。它只是意味着使用提供的字符常量或变量的长度。
    猜你喜欢
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 2014-03-21
    • 2013-06-25
    相关资源
    最近更新 更多