Output formatting: too much whitespace in gfortran

心已入冬 提交于 2019-11-26 12:15:37

问题


Using gfortran 4.6. This code:

PROGRAM f1
IMPLICIT NONE

INTEGER :: i=1, j=3

WRITE(*,*) \"integer i is \", i, \", and j is \", j, \".\"
END PROGRAM f1

produces this console output, which has way too much whitespace:

 integer i is            1 , and j is            3 .

Is there some setting I can set so that there is no space before the first token (\"integer\"), and so the whitespace between tokens is just one space? I know one fix is

WRITE(*,\'(A,I1,A,I1,A)\') \"integer i is \", i, \", and j is \", j, \".\"

but this seems very cumbersome to have to do every time I have a print statement - would rather it be somewhat more like C++ where you explicitly write any whitespace in the output.


回答1:


List-directed IO, i.e, write (*, *) is meant as a convenience. There are no settings to change its behavior. Different compilers will produce different output. Instead you can, as you have identified, use formatted IO. In this case you can use I0 as the format, which will produce the required number of digits, while I1 will only output single-digit integers. Which is OK if those are the largest values that will be output.

WRITE(*,  '( "integer i is ", I0, ", and j is ", I0, "." )' )  i, j



回答2:


You may try some more universal format and re-use it

    fmt = "(*(1x,g0))"

    write(*,fmt) whatever1, whatever2


来源:https://stackoverflow.com/questions/24844250/output-formatting-too-much-whitespace-in-gfortran

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!