Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

write an allocatable array in a file

Mirzaee__Saeed
Beginner
970 Views
hi I have a 2 dimension allocatable array which I need to write each row of the array in one line of output file. the problem is that I don't know the size of array so I can't use formats like '(8(f8.2,x))'. please consider this program: program Console8 implicit none integer(8) :: i,n,m real,allocatable ::bt(:,:) open(unit = 1, file ='output.txt') read*,n,m allocate(bt(n,m)) do i=1,n bt(i,1:m) = i write(1,*) bt(i,1:m) end do end program Console8 for example : the file output.txt for n=2,m=8 will be 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 But I need it to be 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 any idea? Thank you.
0 Kudos
6 Replies
FortranFan
Honored Contributor II
970 Views

Look into the unlimited format item option in the language standard e.g., fmt="(*(F8.2,1X))"

https://software.intel.com/en-us/fortran-compiler-19.0-developer-guide-and-reference-format-specifications

0 Kudos
Greg_T_
Valued Contributor I
970 Views

In addition to the unlimited format item, I've had success using this syntax with a value in the <> brackets before the format type specifier in this snippet example:

numColumn = 8
do row=1,numRow
  write(*,1)array(row,1:numColumn)
end do

1 format(<numColumn>(e16.8))

 

Regards, Greg

0 Kudos
gib
New Contributor II
970 Views

Another option is to use something like this:

write(1,'(100f8.2)') bt(i,1:m)

where 100 is guaranteed to be bigger than any m.  Note that the format can allow more than you use.

Gib

 

0 Kudos
John_Campbell
New Contributor II
970 Views

You could consider less restrictions on the format of output.txt. I would suggest you place more information in the file and make it "easier" to "view". Unless you have a very large matrix to transfer, the extra overhead of the approach below should not be a significant issue.

program Console8

 implicit none
 integer(8) :: i,n,m
 real,allocatable ::bt(:,:)

 open(unit = 11, file ='output.txt')

 read*,n,m
 allocate(bt(n,m))

 write (11,*) n, "  rows of matrix"
 do i=1,n
  write (11,*) i,m
  bt(i,1:m) = i
  do j = 1,m
   write(11,*) bt(i,j)
  end do
 end do

 end program Console8

 

0 Kudos
FortranFan
Honored Contributor II
970 Views

Greg T. wrote:

In addition to the unlimited format item, I've had success using this syntax with a value in the <> brackets before the format type specifier ..

A caveat: this is an extension, not part of the Fortran standard.

0 Kudos
WHARR5
New Contributor I
970 Views

John Campbell's solution is a good one if you wish to avoid formatting.

Here's a tweak of your program using formatting, and Greg T's comment:

program Console8
implicit none
integer(8) :: i,n,m,J !Try n=2; m=8
real,allocatable ::bt(:,:)
!open(unit = 1, file ='output.txt')
read*,n,m
allocate(bt(n,m))
do i=1,n
bt(i,1:m) = i
!write(*,*) bt(i,1:m)
write(*,'(<m>(f9.6))')(bt(i,J),J=1,m)
end do
end program Console8
 
I've added the index J to facilitate the format statement replacing your 'Write' statement (which I've commented out). Also - unlike your 'unit 1' output - I display the output on my output display screen, which accommodates a field width of only 80 characters. So 'm' greater than 8 would wrap around into the next line on that display. I don't know what format you have in mind for larger values of 'm'. Good Luck. 
0 Kudos
Reply