我有以下Fortran 95代码:
program write_test
CHARACTER(LEN=3) :: str
write(*,*) (' ',"""",'File'//trim(str(i)),"""",' ',"""",'Frequency'//trim(str(i)),"""",i=1,5)
end program write_test
!character*(*) function str(k)
character(*) function str(k)
! "Convert an integer to string."
integer, intent(in) :: k
write (str, *) k
str = adjustl(str)
end function str
当我编译并运行它时,我得到以下输出:
" File1" " Frequency1" " File2" " Frequency2" " File3" " Frequency3" " File4" " Frequency4" " File5" " Frequency5"
为什么双引号和字母F之间有空格?
答案 0 :(得分:1)
对于带有*“format”的列表定向输出,编译器可以自由地在打印的项目之间插入一个或多个空格。要获得更精确的控制,请使用格式字符串,如下面的代码所示,使用g95和gfortran进行测试。您可以使用i0格式打印不带空格的整数。
program write_test
character(len=3) :: str
write (*,"(100a)") (" ","""","File"//trim(str(i)),"""", &
" ","""","Frequency"//trim(str(i)),"""",i=1,5)
! preferred way is line below
write (*,"(100(1x,2a,i0,a))") ("""","File",i,"""","""","Frequency",i,"""",i=1,5)
end program write_test
character(*) function str(k)
! Convert an integer to string
integer, intent(in) :: k
write (str,"(i0)") k
str = adjustl(str)
end function str