Is it possible to output a variable with zero value as blank in Fortran?

若如初见. 提交于 2019-12-20 04:13:24

问题


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

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