For Programmers: Free Programming Magazines  


Home > Archive > Fortran > February 2005 > Line break - File I/O problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Line break - File I/O problem
EricT

2005-02-26, 3:59 pm

I'm trying to write a two-dimensional array to an ASCII file.
The problem is that FORTRAN automatically changes line for every 7th
record, instead of writing all the values of one row on one line.
How can I fix this?

beliavsky@aol.com

2005-02-26, 3:59 pm

EricT wrote:
> I'm trying to write a two-dimensional array to an ASCII file.
> The problem is that FORTRAN automatically changes line for every 7th
> record, instead of writing all the values of one row on one line.
> How can I fix this?


Probably you are using list-directed formatting (with the default "*"
format), for which the number of numbers printed on each line depends
on the compiler and is not specified by the Fortran standard. The
following Fortran 95 example prints each row on a separate line by
using a format string. The transpose function is necessary because in
Fortran, matrices are stored by columns. Alternatively, one can use an
implied do loop, as shown. Format strings and implied do loops are
present in Fortran 77 as well as Fortran 95.

integer, parameter :: n = 3
real :: x(n,n)
integer :: i,j
character (len=*), parameter :: fmt_x="(1x,3f5.1)"
forall (i=1:n,j=1:n) x(i,j) = 10.0*i + j
write (*,fmt_x) transpose(x)
write (*,*)
write (*,fmt_x) ((x(i,j),j=1,n),i=1,n) ! uses nested implied do loops
end

output:

11.0 12.0 13.0
21.0 22.0 23.0
31.0 32.0 33.0

11.0 12.0 13.0
21.0 22.0 23.0
31.0 32.0 33.0

beliavsky@aol.com

2005-02-26, 3:59 pm

I should have explained that the format "(1x,3f5.1)" uses a "repeat
edit descriptor" to print 3 real variables, because the matrix has 3
columns in my example. If the number of columns is not known in
advance, the proper format string can be created at run time using an
internal write.

EricT

2005-02-26, 3:59 pm

Thanks!

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com