- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
! Declaration of Variables
CHARACTER(len=20):: InputFile = 'keran.dat'
INTEGER::status ! status = 0 for success
INTEGER::nflows ! No. of flow data values
REAL:: temp ! temp var to read in data
REAL, Allocatable, dimension(:) :: FlowArray
! Open input file:
OPEN (unit=20, File=InputFile, STATUS='OLD', ACTION='READ', POSITION='REWIND',IOSTAT=status)
! Check if file is open
fileopen: If (status /= 0) THEN
WRITE (*,*) 'Hey, I failed to open the file'
GO TO 999
END IF fileopen
DO
READ(20, *,IOSTAT=status) temp
IF (status /= 0) EXIT
nflows = nflows + 1
END DO
ALLOCATE ( FlowArray(nflows),STAT=status)
REWIND (Unit=20)
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Here are two solutions, read_file and read_file2.
The first one is not Fortran 95 standard compliant, it is Fortran 2003 standard, however. But this construct is already implemented in the Compaq and Intel compilers. So in this context it is perfectly legitimate to have ALLOCATABLE as an attribute to a dummy (and actual, of course) argument.
The second solution is Fortran 90, 95 and 2003 standard compliant. It uses pointers.
Lars M
PROGRAM t1
IMPLICIT NONE
REAL, DIMENSION(:), ALLOCATABLE :: Flowarray
REAL, DIMENSION(:), POINTER :: Flowarray2 => NULL()
CALL read_file (Flowarray)
CALL read_file2 (Flowarray2)
CONTAINS
SUBROUTINE read_file (Flowarray)
REAL, DIMENSION(:), INTENT(OUT), ALLOCATABLE :: Flowarray
CHARACTER(len=20):: InputFile = 'keran.dat'
INTEGER::status ! status = 0 for success
INTEGER::nflows ! No. of flow data values
REAL:: temp ! temp var to read in data
! Arrays:
!______________________________________________________
! Open input file:
OPEN (unit=20, File=InputFile, STATUS='OLD', ACTION='READ', POSITION='REWIND',IOSTAT=status)
! Check if file is open
fileopen: If (status /= 0) THEN
WRITE (*,*) 'Hey, I failed to open the file'
STOP
END IF fileopen
! Open was succesful -> count how much data
DO
READ(20, *,IOSTAT=status) temp
IF (status /= 0) EXIT
nflows = nflows + 1
END DO
! Read input file into an array
ALLOCATE ( FlowArray(nflows),STAT=status)
REWIND (Unit=20)
READ (20,*) FlowArray
CLOSE (UNIT=20)
END SUBROUTINE read_file
SUBROUTINE read_file2 (Flowarray)
REAL, DIMENSION(:), POINTER :: Flowarray
CHARACTER(len=20):: InputFile = 'keran.dat'
INTEGER::status ! status = 0 for success
INTEGER::nflows ! No. of flow data values
REAL:: temp ! temp var to read in data
! Arrays:
!______________________________________________________
! Open input file:
OPEN (unit=20, File=InputFile, STATUS='OLD', ACTION='READ', POSITION='REWIND',IOSTAT=status)
! Check if file is open
fileopen: If (status /= 0) THEN
WRITE (*,*) 'Hey, I failed to open the file'
STOP
END IF fileopen
! Open was succesful -> count how much data
DO
READ(20, *,IOSTAT=status) temp
IF (status /= 0) EXIT
nflows = nflows + 1
END DO
! Read input file into an array
ALLOCATE ( FlowArray(nflows),STAT=status)
REWIND (Unit=
20)
READ (20,*) FlowArray
CLOSE (UNIT=20)
END SUBROUTINE read_file2
END PROGRAM t1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page