In Fortran 90, what is a good way to write an array to a text file, row-wise?

前端 未结 3 1198
故里飘歌
故里飘歌 2020-12-14 04:45

I am new to Fortran, and I would like to be able to write a two-dimensional array to a text file, in a row-wise manner (spaces between columns, and each row on its own line)

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-14 05:02

    This may be a very roundabout and time-consuming way of doing it, but anyway... You could simply print each array element separately, using advance='no' (to suppress insertion of a newline character after what was being printed) in your write statement. Once you're done with a line you use a 'normal' write statement to get the newline character, and start again on the next line. Here's a small example:

    program testing
    
    implicit none
    
    integer :: i, j, k
    
    k = 1
    
    do i=1,4
       do j=1,10
          write(*, '(I2,X)', advance='no') k
          k = k + 1
       end do
       write(*, *) ''  ! this gives you the line break
    end do
    
    end program testing
    

    When you run this program the output is as follows:

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

提交回复
热议问题