问题
I would like to output real variables in a formatted file. If the variables are non-zero, format statements are used. But if variables are zero, then only blank spaces are outputted, similar to what Iw.0 does. Is it possible to do this in the format statements? Thank you.
回答1:
No, not with a format statement, but this is reasonably easy to do by writing the values to a string and processing. Below is a demo. Probably better to put into a subroutine.
program demo
real, dimension (6) :: values = [ 1.0, 2.0, 0.0, 4.0, 0.0, 6.0 ]
character (len=100) :: string
integer :: pos
write (string,'( 6 (1X, F4.1 ) )' ) values
write (55, '(A)' ) trim (string)
MakeBlanks: do
pos = index (string, "0.0")
if ( pos < 1 ) exit MakeBlanks
string (pos:pos+2) = " "
end do MakeBlanks
write (55, '(A)' ) trim (string)
end program demo
来源:https://stackoverflow.com/questions/39819102/is-it-possible-to-output-a-variable-with-zero-value-as-blank-in-fortran