Writing data to a file in columns (Fortran)

I need to write some data to a file in Fortran 90. How to use WRITE (*,*) inputto group values ​​in columns? WRITEalways poses new lineafter each call that problem.

Code example

:

open (unit = 4, file = 'generated_trajectories1.dat', form='formatted')

do time_nr=0, N
   write (4,*) dble(time_nr)*dt, initial_traj(time_nr)
end do

And now you need to indicate that it is written in separate columns.

+5
source share
4 answers

You can use implied DO stacks to write values ​​as single entries. Compare the following two examples:

integer :: i

do i=1,10
   write(*,'(2I4)') i, 2*i
end do

It produces:

1   2
2   4
3   6
...

Using the implied DO stacks, it can be rewritten as:

integer :: i

write(*, '(10(2I4))') (i, 2*i, i=1,10)

It produces:

1   2   2   4   3   6   ...

If the number of elements is not fixed at compile time, you can use the extension <n>(not supported gfortran):

write(*, '(<n>(2I4))') (i, 2*i, i=1,n)

(2I4) n. GNU Fortran , :

character(len=20) :: myfmt

write(myfmt, '("(",I0,"(2I4))")') n
write(*, fmt=myfmt) (i, 2*i, i=1,n)

, ( *):

write(*, *) (i, 2*i, i=1,10)
+9

, (.. ...). ?

, -, advance="no" ,

integer :: x

do x = 1,10
  write(*, fmt='(i4,1x)', advance="no") x
end do

, -.

+3

. , IO, . , , .

, , , -, (*) IO. - . . , , .

, , reals:

write (4, '( *(2X, ES14.6) )', advance="no" )
0

$edit:

write(*, fmt='(i4,$)') x

(*, *) ...

0

All Articles