I'm having an odd problem... All I'm doing is reading an external file into an allocatable array..
I have a do loop take count of the lines in the external file (as it will vary). I tested the counting code first to ensure it was working. It counted 55626 lines, the correct size.
I then added the extra code to allocate an array to that size and proceed to read the data into the array.. HOWEVER once I add the extra code, the program counts 53482?? The program doesn't break, it finishes as though thats the last line.
If I replace the "count" variable in the "writing" do loop with the correct number of lines (55626) then I get the IOSTAT error STOP I have built in.
Any ideas?? The file is 55626 lines and 14 columns..
MY CODE:
PROGRAM ARRAY
IMPLICIT NONE
INTEGER,ALLOCATABLE :: HIST,
CHARACTER,ALLOCATABLE :: ITM)
CHARACTER(7) :: ITEM
INTEGER :: count=0, ierror=0, i, YEAR, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
WRITE(*,'*) "READING FILE LENGTH....."
OPEN(UNIT=20, FILE="ITEMHIST.CSV", STATUS="old", IOSTAT=ierror)
IF (ierror/=0) STOP "Error opening file"
DO
READ(20,*,IOSTAT=ierror) ITEM, YEAR, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
IF (ierror/=0) EXIT
count=count+1
END DO
ierror = 0
REWIND(20)
OPEN(UNIT=30, FILE="OUTPUT.DAT", ACTION="WRITE", STATUS="REPLACE", IOSTAT=ierror)
IF (ierror/=0) STOP "Error creating or replacing OUTPUT.DAT"
WRITE(*,*) "NUMBER OF LINES IN FILE: ", count
WRITE(*,*)
WRITE(*,*) "WRITING TO OUTPUT.DAT"
WRITE(*,*)
ALLOCATE( HIST( count, 13 ), stat=ierror)
IF (ierror/=0) STOP "Error allocating array"
ALLOCATE( ITM( count ), stat=ierror)
IF (ierror/=0) STOP "Error allocating array"
DO i=1,count
READ(20,*,IOSTAT=ierror) ITEM, YEAR, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
IF (ierror/=0) STOP "ERROR READING ITEMHIST.CSV"
ITM(i) = ITEM
HIST(i,1) = YEAR
HIST(i,2) = JAN
HIST(i,3) = FEB
HIST(i,4) = MAR
HIST(i,5) = APR
HIST(i,6) = MAY
HIST(i,7) = JUN
HIST(i,8) = JUL
HIST(i,9) = AUG
HIST(i,10) = SEP
HIST(i,11) = OCT
HIST(i,12) = NOV
HIST(i,13) = DEC
WRITE(*,'(A7)',ADVANCE='NO') ITM(i)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,1)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,2)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,3)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,4)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,5)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,6)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,7)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,8)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,9)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,10)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,11)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,12)
WRITE(*,'(I10)') HIST(i,13)
WRITE(30,'(A7)',ADVANCE='NO') ITM(i)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,1)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,2)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,3)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,4)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,5)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,6)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,7)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,8)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,9)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,10)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,11)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,12)
WRITE(30,'(I10)') HIST(i,13)
END DO
WRITE(*,*) "LINES COUNTED IN DOCUMENT: ", count
WRITE(30,*) "LINES COUNTED IN DOCUMENT: ", count
DEALLOCATE(HIST, stat=ierror)
IF (ierror/=0) STOP "Error deallocating array"
DEALLOCATE(ITM, stat=ierror)
IF (ierror/=0) STOP "Error deallocating array"
CLOSE(20)
CLOSE(30)
END PROGRAM
I have a do loop take count of the lines in the external file (as it will vary). I tested the counting code first to ensure it was working. It counted 55626 lines, the correct size.
I then added the extra code to allocate an array to that size and proceed to read the data into the array.. HOWEVER once I add the extra code, the program counts 53482?? The program doesn't break, it finishes as though thats the last line.
If I replace the "count" variable in the "writing" do loop with the correct number of lines (55626) then I get the IOSTAT error STOP I have built in.
Any ideas?? The file is 55626 lines and 14 columns..
MY CODE:
PROGRAM ARRAY
IMPLICIT NONE
INTEGER,ALLOCATABLE :: HIST,
CHARACTER,ALLOCATABLE :: ITM)
CHARACTER(7) :: ITEM
INTEGER :: count=0, ierror=0, i, YEAR, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
WRITE(*,'*) "READING FILE LENGTH....."
OPEN(UNIT=20, FILE="ITEMHIST.CSV", STATUS="old", IOSTAT=ierror)
IF (ierror/=0) STOP "Error opening file"
DO
READ(20,*,IOSTAT=ierror) ITEM, YEAR, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
IF (ierror/=0) EXIT
count=count+1
END DO
ierror = 0
REWIND(20)
OPEN(UNIT=30, FILE="OUTPUT.DAT", ACTION="WRITE", STATUS="REPLACE", IOSTAT=ierror)
IF (ierror/=0) STOP "Error creating or replacing OUTPUT.DAT"
WRITE(*,*) "NUMBER OF LINES IN FILE: ", count
WRITE(*,*)
WRITE(*,*) "WRITING TO OUTPUT.DAT"
WRITE(*,*)
ALLOCATE( HIST( count, 13 ), stat=ierror)
IF (ierror/=0) STOP "Error allocating array"
ALLOCATE( ITM( count ), stat=ierror)
IF (ierror/=0) STOP "Error allocating array"
DO i=1,count
READ(20,*,IOSTAT=ierror) ITEM, YEAR, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
IF (ierror/=0) STOP "ERROR READING ITEMHIST.CSV"
ITM(i) = ITEM
HIST(i,1) = YEAR
HIST(i,2) = JAN
HIST(i,3) = FEB
HIST(i,4) = MAR
HIST(i,5) = APR
HIST(i,6) = MAY
HIST(i,7) = JUN
HIST(i,8) = JUL
HIST(i,9) = AUG
HIST(i,10) = SEP
HIST(i,11) = OCT
HIST(i,12) = NOV
HIST(i,13) = DEC
WRITE(*,'(A7)',ADVANCE='NO') ITM(i)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,1)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,2)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,3)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,4)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,5)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,6)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,7)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,8)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,9)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,10)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,11)
WRITE(*,'(I10)',ADVANCE='NO') HIST(i,12)
WRITE(*,'(I10)') HIST(i,13)
WRITE(30,'(A7)',ADVANCE='NO') ITM(i)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,1)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,2)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,3)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,4)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,5)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,6)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,7)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,8)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,9)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,10)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,11)
WRITE(30,'(I10)',ADVANCE='NO') HIST(i,12)
WRITE(30,'(I10)') HIST(i,13)
END DO
WRITE(*,*) "LINES COUNTED IN DOCUMENT: ", count
WRITE(30,*) "LINES COUNTED IN DOCUMENT: ", count
DEALLOCATE(HIST, stat=ierror)
IF (ierror/=0) STOP "Error deallocating array"
DEALLOCATE(ITM, stat=ierror)
IF (ierror/=0) STOP "Error deallocating array"
CLOSE(20)
CLOSE(30)
END PROGRAM