Fortran: add column to file (i.e. skip a varyi

2019-07-06 04:52发布

问题:

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!