How to Fix Runtime File Reading Error in Fortran Code?

In summary, the code appears to be working, but I am not sure why the REWIND statement is not helping me.
  • #1
chaim
5
0
I frequently get the same runtime error when using g77. Here is the error message:

fmt: end of file
apparent state: unit 16 named vi1.src
last format: (F10.6)
lately reading sequential formatted external IO

abnormal program termination

I have added to my code the REWIND statement after my OPEN statement as you can see in the code below but this has not helped me. I know that the error means that the pointer is at the end of the input file, but I think that the REWIND should solve this problem. Where am I going wrong??

Code:

PROGRAM CloudCoverV1
INTEGER NH,TR,ACC
DIMENSION VI(8788), VF(8788)
c=================================================================
OPEN(UNIT=16, FILE='vi1.src', ACCESS='SEQUENTIAL',STATUS='OLD',
$FORM='FORMATTED')
REWIND (UNIT=16)
c=================================================================



READ(UNIT=16, FMT=55) NH
55 FORMAT (I5)
DO 60, NH = 1,NH
READ(UNIT=16, FMT=56) VI
56 FORMAT (F10.6)
60 CONTINUE
c====================================================================
OPEN(UNIT=18, FILE='test1.out', STATUS='NEW', FORM='FORMATTED')
c=====================================================================

DO 110, TR = 1, 20
WRITE(UNIT=17, FMT=111) VI(NH)
111 FORMAT (F10.6)
110 CONTINUE


c====================================================================
OPEN(UNIT=17, FILE='CCV1.out', STATUS='NEW', FORM='FORMATTED')
c=====================================================================

DO 12, ACC = 1,NH

IF (VI(NH) .LT. 100) VF(NH) = VI(NH)
IF (VI(NH) .LT. 100) GOTO 12

IF (VI(NH) .EQ. 100) THEN
CALL INTERPOLATE(NH)

ENDIF
12 CONTINUE

c=================================================================
DO 120, NH = 1, 100
WRITE(UNIT=17, FMT=100) VF(NH)
100 FORMAT (F10.6)
120 CONTINUE
c===================================================================
CLOSE(UNIT=16)
CLOSE(UNIT=17)
CLOSE(UNIT=18)

END PROGRAM CloudCoverV1




SUBROUTINE INTERPOLATE(NH)
INTEGER NH
INTEGER NUME,VAL
DIMENSION VI(8788)
DO 80,NH = NH,NH+24
NUME = 0
IF (VI(NH) .EQ. 100) NUME = NUME + 1
IF (VI(NH) .NE. 100) EXIT
80 CONTINUE
IF ((VI(NH-NUME)- VI(NH+1)) .GT. 0.0) THEN
DO 77, VAL = NH-NUME,NH
VI(VAL) = VI(NH-NUME)-((VI(NH-NUME)- VI(NH+1))/(NUME+1))
77 CONTINUE

ELSEIF ((VI(NH-NUME)- VI(NH+1)) .LT. 0.0) THEN
DO 99, VAL = NH-NUME,NH
VI(VAL)=VI(NH-NUME)+((VI(NH+1)- VI(NH-NUME))/(NUME+1))
99 CONTINUE
ENDIF
RETURN
END SUBROUTINE INTERPOLATE
 
Technology news on Phys.org
  • #2
Caveat: The last fortran programming I did was about 15 years ago.
Your REWIND statement comes right after you open the input file. When you open the file, the file pointer is pointing to the first data in the file, so rewinding doesn't move the file point, so won't affect anything.

In any case, rewinding the file is just a kludge to paper over the real problem, which I believe is related to the line below.

This line is very bad:
Code:
DO 60, NH = 1,NH
You don't want your loop control variable to be the same as the starting or ending value.
 
  • #3
Yes, Mark44, I appreciate your comment, but the loop has nothing to do with the runtime abend. I still need to know the reason for the abend. Especially what is the meaning of the first line:

fmt: end of file
 
  • #4
I believe that the loop has everything to do with your error. Is abend German for error?

I don't have a fortran compiler any more, so I can't test the behavior of that loop, but you need to fix it. The first number in your input file, vi1.src, indicates the number of items that follow it.

The loop control variable should be, say, I, so that the loop executes NH times. That way the loop will stop reading input values at the right place, and you won't have the error of trying to read past the end of data in your file.

Instead of this
Code:
DO 60, NH = 1,NH

change to this
Code:
DO 60, I = 1,NH
 
  • #5
Mark44, I made the changes that you suggested and I still get the same error. I would like to add that I get this error with different programs. The odd thing is that "sometimes" the program works and then stops working. I think that the external data file is in some way being corrupted, but I cannot figure out what is happening.
 
  • #6
I didn't notice this before, but I'm pretty sure the line marked with <--- is causing you problems as well.

Code:
DIMENSION VI(8788), VF(8788)
.
.
.

READ(UNIT=16, FMT=55) NH
55 FORMAT (I5)
DO 60, NH = 1,NH 
READ(UNIT=16, FMT=56) VI <---
56 FORMAT (F10.6)
60 CONTINUE

You should be reading from the input file to one element of your array; i.e., to VI(I), assuming you are using I for the loop control variable for that loop.

There are a number of other errors in your code.
1.
Code:
DO 110, TR = 1, 20
WRITE(UNIT=17, FMT=111) VI(NH)
111 FORMAT (F10.6)
110 CONTINUE
This loop writes the same value, VI(NH), to your output file 20 times. I doubt this is what you intended. Notice that TR starts off at 1 and is incremented by 1 each time until it gets to 21, the first value that is too large. The apparent intent of this loop is to write the first 20 values of the VI array to the output file.

2. The block below has a similar problem.
Code:
DO 12, ACC = 1,NH

IF (VI(NH) .LT. 100) VF(NH) = VI(NH)
IF (VI(NH) .LT. 100) GOTO 12 

IF (VI(NH) .EQ. 100) THEN
CALL INTERPOLATE(NH)

ENDIF
12 CONTINUE
Everywhere you have VI(NH) or VF(NH), you should be using your loop control variable, ACC. IOW, where it says VI(NH), it should be VI(ACC). Same for the VF array.

3. Same with this block.
Code:
DO 120, NH = 1, 100 
WRITE(UNIT=17, FMT=100) VF(NH) 
100 FORMAT (F10.6) 
120 CONTINUE

4. In your subroutine you are declaring an array named VI. This array is different from the one you have declared above it, even though both have the same name and the same number of elements. You should be passing this array to your subroutine as a parameter.

5. In the subroutine again you have this loop.
Code:
DO 80,NH = NH,NH+24
NUME = 0
IF (VI(NH) .EQ. 100) NUME = NUME + 1
IF (VI(NH) .NE. 100) EXIT
80 CONTINUE
The line DO 80 NH = NH, NH + 24 is bad for at least a couple of reasons:
a) NH is a parameter passed into your subroutine. It should not also be used as a loop control variable.
b) Setting a variable to its own value is always silly.
 
  • #7
Mark44, Yes after thourough checking of the program you are correct. I was trying to read more records than existed in the input file. Also all your remarks concerning my syntax problems will be corrected. Just one question that you may be able to help me with. How can I make my arrays VI(8788) and VF(8788) accessable to both the main program and the subroutine. That is I would like both the main and the subroutine to be able to update both of the arrays?
 
  • #8
Something like this:
Code:
PROGRAM CloudCoverV1
INTEGER NH = 8788
c - other declarations

REAL VI(NH), VF(NH)
.
.
.
CALL INTERPOLATE(VI, NH)
...
END PROGRAM CloudCoverV1


SUBROUTINE INTERPOLATE(Array, NH)
INTEGER NH
REAL Array(NH)
c Declarations of subroutine local variables
.
.
.
 
RETURN 
END SUBROUTINE INTERPOLATE
 

1. What is a runtime file reading error?

A runtime file reading error is an error that occurs while a program is running and trying to read a file. It usually happens when the program encounters unexpected or invalid data in the file.

2. What causes a runtime file reading error?

A runtime file reading error can be caused by a variety of factors, including incorrect file formats, missing or corrupted files, and programming errors. It can also occur when there is a mismatch between the program and the file being read.

3. How can I fix a runtime file reading error?

The best way to fix a runtime file reading error is to identify the cause of the error and address it. This may involve checking the file format, ensuring that all necessary files are present and not corrupted, and debugging any programming errors. It may also be helpful to consult online resources or seek assistance from other programmers.

4. Is a runtime file reading error the same as a syntax error?

No, a runtime file reading error is not the same as a syntax error. A syntax error occurs when the code written by the programmer does not follow the rules of the programming language. A runtime file reading error, on the other hand, occurs during program execution and is caused by problems with reading a file.

5. How can I prevent runtime file reading errors?

To prevent runtime file reading errors, it is important to thoroughly test your code and ensure that it can handle different file formats and data types. It is also helpful to include error handling mechanisms in your code to catch and handle any unexpected errors that may occur during runtime. Properly documenting your code and using a consistent and organized file structure can also help prevent runtime file reading errors.

Similar threads

  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
17
Views
4K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
2
Views
7K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
2
Views
8K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
4
Views
5K
Back
Top