Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to read different columns

Status
Not open for further replies.

fanta2

Technical User
Apr 10, 2005
78
CA
I have different records having different columns how can i read them in formatted reading.
Example
1 2 3 4 (4 columns)
1 23 5 (3 columns)
2 36 58 78 52 (5 columns)
I defined the column based on the max column but the read statement reads 0 for unavilable value
 
Use implicit loop element in READ stmt list.
 
I can use implicit loop but the number of columns is random.

say
integer :: values (norows, nocols)
do i=1, norows
read(*,*) (values (norows, nocols),j=1,ncols)
end do
this needs array of size norows * nocols but my array has no constant columns........
 
What you need is a double read - first from the file into a buffer then from the buffer into the array. Something like this
Code:
      program main
!     MAXCOL is 1 more than the max expected
      parameter (MAXCOL = 10, INFILE = 5)
      integer info(MAXCOL)
      character*80 line
      integer row, col
      open (INFILE, file='columns.txt', status='old')
      row = 0
10    continue
         row = row + 1
!
!        Read the line containing the data
         read (INFILE, '(A)', end=30) line
!
!        Read the data from the line
         read (line, *, err=20, end=20) (info(col), col = 1, MAXCOL, 1)
20       continue
!
!        we stop at one more that what was there so subtract 1
         col = col - 1
!
!        print what we got
         write (*, 25) row, col
25       format ('Row ',I3,' Columns ',I2)
         write (*, *) (info(i), i = 1, col, 1)
         goto 10
30    continue
      close (INFILE)
      stop
      end
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top