Q:

How to read a text file into an array in Fortran

Is there any way to condense this code?
.
.
real*4 a4,e4,inc4,capom4,omega4,capm4
integer*2 id2
real*8 array(1e7,8)
.
.
row=0
do n=1,nleft+nbod2-1
 row=row+1
 read(iu) id2,a4,e4,inc4,capom4,omega4,capm4    
 array(row,1)=id2
 array(row,2)=a4
 array(row,3)=e4
 array(row,4)=inc4
 array(row,5)=capom4
 array(row,6)=omega4
 array(row,7)=capm4
end do
.
.

I've tried about five different ways starting with an implied DO loop and all result in "forrtl: severe (67): input statement requires too much data...".

A:

Since the OP's code involves mixed data types including integer*2, direct use of read(iu) array( n, 1:7 ) seems not working. So I made another two trials to condense the code. The first one is
read(iu) id2, a4, e4, inc4, capom4, omega4, capm4
array( n, : ) = [ real*8 :: id2, a4, e4, inc4, capom4, omega4, capm4 ]

which saves some lines of code, but still a bit lengthy. So the second trial is
type dat_t
    sequence
    integer*2 :: id2
    real*4    :: a4, e4, inc4, capom4, omega4, capm4
endtype

type(dat_t) :: dat( 10000 )   !! or allocate() if necessary

and read the unformated file as
do n = 1, nleft + nbod2 - 1
    read(iu) dat( n )
enddo

The obtained data can be used as usual, e.g., dat( n )% a4. Here it is important to put the keyword sequence to ensure contiguous arrangement of variables in memory. Otherwise the same error occurs as in the OP's case...
I have tested this with a code below (using gfortran4.8 and ifort14.0)
program main
    integer, parameter :: dp = 8

    type dat_t
        sequence                                                                   
        character(5) :: s
        integer      :: k
        real(dp)     :: x, y
    endtype

    type(dat_t) :: d( 500 )
    real(dp)    :: a( 100 )

    !! write test data                                                              
    open( 10, file="test.dat", form="unformatted" )
    write( 10 ) "hello", 100, 777.0_dp, 888.0_dp
    close( 10 )

    !! read test data                                                               
    open( 20, file="test.dat", form="unformatted", status="old" )

    read( 20 ) d( 50 )
    print *, d( 50 )

    a( 1:3 ) = [ real(dp) :: d( 50 )% k, d( 50 )% x, d( 50 )% y ]
    print *, a( 1:3 )

    close( 20 )
end

