At 17:25 1998-10-19 +0200, you wrote:
>
>I have a list of integers file with the following information. The first
>line has two integres - the total number of lines in the file and the
>total number of integres listed in the file.
>Then comes the list of integers, listed line after line. The number of
>integers per line is not known before hand.
>
>Example
>7 22
>5 3 2
>1 3 4
>5 4 2 1
>2 3 6 7
>1 3 6
>5 4 7
>6 4
>
>I want to read this file and develop a datastructure in which the numbers
>are read into an array (called total) of exact size and the number of
>integers per line is registered in another array (called line).
>This is how I started.
>Read the first line and allocate the arrays total and line.
>For the above example, the following is done
> allocate(total(22))
> allocate(line(7))
>
>Reading the rest of the file into total is easy. I have a problem with
>reading data into line. How does one read a list of input of unknown size
>in fortran90?
>
>regards
>Assefaw Hadish
>
>
>
>
>
You could do something like this:
PROGRAM integers
IMPLICIT NONE
INTEGER :: n_lines, n_total, i
INTEGER, ALLOCATABLE, DIMENSION(:) :: per_line, total
OPEN (UNIT=1, FILE="A.DAT", ACTION="READ")
READ (1, *) n_lines, n_total
ALLOCATE (per_line(1:n_lines), total(1:n_total))
READ (1, *) total ! Read in whole array "total".
REWIND (UNIT=1) ! Back to beginning.
READ (1, '()') ! Skip first line, already dealt with.
DO i=1, n_lines
per_line(i) = n_integers_on (1) ! Count numbers per line.
END DO
CLOSE (UNIT=1)
.
. Use "per_line" and "total".
.
DEALLOCATE (per_line, total)
CONTAINS
FUNCTION n_integers_on (unit) RESULT (n)
INTEGER, INTENT(IN) :: unit
INTEGER :: n
!
! This function interprets a line as a growing list of integers until
it fails,
! and returns the number of successfully interpreted integers as result
"n".
CHARACTER(LEN=512) :: line
INTEGER :: istat, iw, i
READ (unit, '(A)', IOSTAT=istat) line
n = 1
DO
READ (line, *, IOSTAT=istat) (iw, i=1, n)
IF (istat==0) THEN
n = n + 1
ELSE
! "n" is one too many now, so:
n = n - 1 ! and
EXIT ! out of loop ----------------------------->
ENDIF
END DO
END FUNCTION n_integers_on
END PROGRAM integers
Best wishes
Lars Mossberg
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|