0

Here is an attempt to read 10 white-space-separated integer values from a file into an array:

subroutine ReadFileIntoArray (filename, int_array)
    implicit none

    character(len = *), intent (in) :: filename
    integer, dimension(:), intent(in out) :: int_array

    integer :: current_index

    open (1, file = filename, status = 'old')

    do current_index = 1, 10, 1
        read(1, *) int_array(current_index)
    end do

    close (1)
end subroutine ReadFileIntoArray

! ---------------------------------------------------------------------- !

program ReadFileCalculateAverage
    implicit none

    integer, dimension(10) :: int_array
    character (len = 9) :: filename = 'makrs.txt'

    call ReadFileIntoArray(filename, int_array) ! <--- error line

end program ReadFileCalculateAverage

The error I'm getting (using Code::Blocks 16.01 on Windows 10) is:

Error: explicit interface required for 'ReadFileIntoArray' : assumed-shape argument

Having in mind my limited knowledge of Fortran, what is the way to correct the above code? Are there any other significant (still unobserved) errors that I should correct?

Ziezi
  • 6,375
  • 3
  • 39
  • 49

1 Answers1

1

You will eventually want a unit number other than 1 for the open and the read. Maybe 21 or use NEW UNIT.

For the FileName I usually use LEN=256 everywhere, and then on the open use

INTEGER :: LUN
...
OPEN(NEWUNIT=LUN, FileName(1:LEN_TRIM(FileName)) )

Another option is to have FileName declared in a module with the subroutine, and don't pass it

MODULE AA
IMPLICIT NONE
CHARACTER(LEN=256), PUBLIC :: FileName
CONTAINS
subroutine readarray(array)
...
ENDMODULE AA

Or give something for the *

SUBROUTINE readarray(FileName,array,lenF)
iNTEGER,INTENT(IN   )           :: LenF
CHARACTER(LEN=LenF), INTENT(IN) :: FileName 
Holmz
  • 714
  • 7
  • 14