Fortran 写入输出中的奇怪字符
strange character in Fortran write output
我想为一些子程序计时。这是我用来写名字和执行时间的模板:
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 �>
为什么结束时间-开始时间打印的是奇怪的字符而不是数值?顺便说一句,pout() 只是以线程安全的方式将缓冲区写入特定于进程的文件。它不应该与问题有任何关系,但如果这里没有其他东西会导致错误输出,那么我可以 post 它的主体。
你搞错了!该行应显示为
write (timebuf, '(a30,e20.10e3)') 'get_sigma_vrelp',endtime-starttime
这样一来,您期望的是一个长度为 30 个字符的字符串 (a30
),而不是 30 个任意长度的字符串 (30a
)。 write
语句接收的不是第一个字符串之后的字符,而是float对应的字节。因此垃圾。
你的字面量只有 15 个字符长,所以你可以把这行写成
write (timebuf, '(a15,e20.10e3)') 'get_sigma_vrelp',endtime-starttime
或者让编译器自己决定长度:
write (timebuf, '(a,e20.10e3)') 'get_sigma_vrelp',endtime-starttime
我想为一些子程序计时。这是我用来写名字和执行时间的模板:
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 �>
为什么结束时间-开始时间打印的是奇怪的字符而不是数值?顺便说一句,pout() 只是以线程安全的方式将缓冲区写入特定于进程的文件。它不应该与问题有任何关系,但如果这里没有其他东西会导致错误输出,那么我可以 post 它的主体。
你搞错了!该行应显示为
write (timebuf, '(a30,e20.10e3)') 'get_sigma_vrelp',endtime-starttime
这样一来,您期望的是一个长度为 30 个字符的字符串 (a30
),而不是 30 个任意长度的字符串 (30a
)。 write
语句接收的不是第一个字符串之后的字符,而是float对应的字节。因此垃圾。
你的字面量只有 15 个字符长,所以你可以把这行写成
write (timebuf, '(a15,e20.10e3)') 'get_sigma_vrelp',endtime-starttime
或者让编译器自己决定长度:
write (timebuf, '(a,e20.10e3)') 'get_sigma_vrelp',endtime-starttime