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

Reading automatic arrays

Status
Not open for further replies.

Zemm

Technical User
Apr 22, 2006
6
US
I am running Fortran 90 in DOS and trying to read one-dimensional arrays from an input file. These are the first few lines in the file.

Sample Influent Effluent
Date BOD (mg/l) BOD (mg/l)
01/22/88 203.1 7.6
01/31/88 159.0 15.1
02/05/88 214.1 17.5

In the code, the variable X is for the Influent column and Y is for the Effluent column. I don't know how many rows of data are in the file, so the arrays need to be automatic. When I run it, I get this error message:

"The instruction at address 4534636 attempted to read from an illegal location
00560982IO_process_[void][+006c]
00508eaeCH_RSF [02F0]
00041001 main [+0141]"

Code:
PROGRAM Lab3c
! ----------------------------------------------------------------
! This program computes a linear correlation coefficient to find a
! linear correlation between waste input and waste output at a
! waste treatment plant. 
! Data: Input from file
! ----------------------------------------------------------------
IMPLICIT NONE
INTEGER :: N, Sum, EOF, NMax
! DOUBLE PRECISION :: Cin, Cout, CinMean, CoutMean, Rho
CHARACTER, DIMENSION(:), ALLOCATABLE :: Char
INTEGER, DIMENSION(:), ALLOCATABLE :: X, Y

OPEN (UNIT=14, FILE="bodinout.dat", STATUS="OLD")
OPEN (UNIT=16, FILE="bodinout.out", STATUS="OLD")
N = 1
SUM = 0D0
DO
   READ(14, Char, IOSTAT = EOF)Char(N), X(N), Y(N)
write (16,*) "In the do loop before eof if statement and N = ", N, " Char = ", Char(N) !, " X = ", X(N)
   IF(EOF /= 0) THEN
write (16,*) "In the if check before exit "
   EXIT
END IF 
   IF(N == Nmax + 1) THEN
        WRITE(*,*) "Too many data points", Nmax
        STOP
   END IF
   Sum = Sum + X(N)
   N = N+1
WRITE (16,*) "X = ", X(N), "and sum = ", Sum 
END DO
   N = N-1     ! because N is one step ahead the whole time
   
END PROGRAM Lab3c
I have code for an interface, which calls a function, before the OPEN statements. But the function is used for calculation, which it can't do until I can get the data from the file, so I decided not to paste that part of the code.

The compiler complains when I put more than one variable name in the read statement. Like this:
Code:
READ(14, Char, X, IOSTAT = EOF)Char(N), X(N), Y(N)
It says a "Read statement can have a maximum of 2 non-keyword specifiers."

Why can't I read this arrays? Thank you - Zemm
 
What are Char and X meant to be in
Code:
READ(14, Char, IOSTAT = EOF)Char(N), X(N), Y(N)
READ(14, Char, X, IOSTAT = EOF)Char(N), X(N), Y(N)
You have Char in two places. The first place is meant to be a format and the second the variable you're reading in. Try changing to
Code:
READ(14,*, IOSTAT=EOF) Char(N), X(N), Y(N)
 
I changed it to this:
Code:
READ(14, *, IOSTAT = EOF) CHAR(N), X(N), Y(N)
WRITE (16, *) "X = ", X(N), " Y = ", Y(N)
I still get a runtime error (below), but it's changed to a WRITE problem, whereas it was a READ problem before.

"The instruction at address 4534636 attempted to write to location e0982eceb"

I don't see anything wrong with the WRITE statement.
 
At a guess your write statement should be after the if that checks for end of file.

How much data is written out? Is it everything up to EOF or nothing at all?

Also if X and Y are allocatablee, where is your allocate statement? Your array is out of bounds if there wasn't an allocate before usage.
 
Thank you again for the help. This program doesn't need an allocate statement. And I don't understand exactly why not because I haven't written a program with that statement.
I put the write statement after the IF that checks for end-of-file.

Another problem was that X and Y were actually INTEGER arrays in the program, but double precision in the data file. And I have to tell fortran which columns to read for X and Y...
Code:
READ(14,"(T17,F5.1,T25,F4.1)",IOSTAT = EOF)X(N),Y(N)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top