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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Odd behavior when reading external file into array

Status
Not open for further replies.

doktord

Technical User
Sep 26, 2006
24
US
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
 
Well after some problem solving, I find that I encounter my problem when I set "YEAR, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC" as integers (or reals) instead of characters...

But the data that is contained in these columns of the file are numbers as follows:

2008,0000033,0000028,0000019,0000010,0000008,-00004,0000037,0000028,0000042,0000017,0000013,0000027,

Any thoughts on what I should do or what the problem could be? I need to use this data later in my program for calculations.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top