Fortran: add column to file (i.e. skip a varying amount of horizontal spaces)

丶灬走出姿态 提交于 2019-12-23 18:14:40

问题


I'm a bloody beginner to Fortran (f90) and some apparently easy problems turn out to cause severe headaches...Thanks for helping me with this one:

My code runs through a loop, processes data and writes them into a file. I'd like to have those data written in columns of the same file until the looping is finished.

OPEN (unit=11,file=filename // '.csv')
WRITE(11,'(i4,A1,f10.6)') NUM4 , tab, NUMfloat10_6
CLOSE(11)

This code works fine for the saving of a single dataset. "tab" is defined as char(9); filename is specified by the user at the beginning of the script.

When in loop-mode I'd like to add another tab as "A1" and another NUMfloar10_6 ("f10.6"). However, I can't so something like this:

OPEN (unit=11,file=filename // '.csv')
WRITE(11,'(Tk,i4,A1,f10.6)') NUM4 , tab, NUMfloat10_6
CLOSE(11)

with k defined as an integer, increasing with number of loop * 15.

How do you I solve that problem? How do I "add" columns to a file without knowing how many spaces to skip?


回答1:


In case you use ifort, just add brackets <k>

  k = 2
  WRITE(*,'(T<k>,A)'), "Hello World!"

  k = 6
  WRITE(*,'(T<k>,A)'), "Hello World!"

  k = 16
  WRITE(*,'(T<k>,A)'), "Hello World!"

produces:

 Hello World!
     Hello World!
               Hello World!


来源:https://stackoverflow.com/questions/24157423/fortran-add-column-to-file-i-e-skip-a-varying-amount-of-horizontal-spaces

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