user2145510
user2145510

Reputation: 67

Fortran output on two lines instead of one

I'm running a fortran 90 program that has an array of alpha values with i=1 to 40. I'm trying to output the array into 5 rows of 8 using the code below:

write(4,*) "alpha "
write(4,*)alpha(1), alpha(2), alpha(3), alpha(4), alpha(5), alpha(6), alpha(7), alpha(8)
write(4,*)alpha(9), alpha(10), alpha(11), alpha(12), alpha(13), alpha(14), alpha(15), alpha(16) 
write(4,*)alpha(17), alpha(18), alpha(19), alpha(20), alpha(21), alpha(22), alpha(23), alpha(24)
write(4,*)alpha(25), alpha(26), alpha(27), alpha(28), alpha(29), alpha(30), alpha(31), alpha(32)
write(4,*)alpha(33), alpha(34), alpha(35), alpha(36), alpha(37), alpha(38), alpha(39), alpha(40)

where 4 is the desired output file. But when I open the output, there are 10 rows instead of 5 each with 5 values then 3 values alternating. Any idea what I can do to avoid this?

Thanks.

Upvotes: 2

Views: 313

Answers (2)

Beliavsky
Beliavsky

Reputation: 318

write (4,"(8(1x,f0.4))") alpha

prints the 40 numbers over 5 lines, because in Fortran "format reversion" the format is re-used when you reach the end of it, with further data printed on a new line.

The site http://www.obliquity.com/computer/fortran/format.html says this about format reversion:

"If there are fewer items in the data transfer list than there are data descriptors, then all of the unused descriptors are simply ignored. However, if there are more items in the data transfer list than there are data descriptors, then forced reversion occurs. In this case, FORTRAN 77 advances to the next record and rescans the format, starting with the right-most left parenthesis, including any repeat-count indicators. It then re-uses this part of the format. If there are no inner parenthesis in the FORMAT statement, then the entire format is reused."

Upvotes: 4

M. S. B.
M. S. B.

Reputation: 29381

Use formatted IO. List-directed IO (i.e., with "*") is designed to be easy but is not fully specified. Different compilers will produce different output. Try something such as:

write (4, '( 8(2X, ES14.6) )' )  alpha (1:8)

Or use a loop:

do i=1, 33, 8
   write (4, '( 8(2X, ES14.6) )' )  alpha (i:i+7)
end do

Upvotes: 4

Related Questions