Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

fortran - Output formatting: too much whitespace in gfortran

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...