Io Fortran写输出中的奇怪字符

Io Fortran写输出中的奇怪字符,io,formatting,fortran,mpi,Io,Formatting,Fortran,Mpi,我想给一些子程序计时。以下是我用来编写执行名称和持续时间的模板: 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)'

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

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()只是以线程安全的方式将缓冲区写入特定于进程的文件。它不应该与问题有任何关系,但是如果这里没有其他任何东西会导致错误的输出,那么我可以发布它的正文。

您的方法是错误的!这行应该是

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可以。它只是意味着使用提供的字符常量或变量的长度。