Solve FORTRAN READ Problems: Troubleshooting & Manual

  • Context: Fortran 
  • Thread starter Thread starter jasonbot
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a FORTRAN code issue related to file handling, specifically an error encountered during a read operation after the end-of-file (EOF) marker. Participants explore the code's behavior, suggest solutions, and share resources for learning FORTRAN.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • The original poster describes receiving a FORTRAN runtime error related to reading from a file that has just been created and is empty.
  • One participant suggests that the issue arises because the file is opened with status='unknown', leading to an EOF condition immediately after creation, preventing subsequent writes.
  • A proposed solution involves adding a REWIND statement after a specific line in the code to reset the file pointer before writing.
  • Another participant notes that the behavior may be a quirk of the gfortran compiler, as they experienced normal execution with a different compiler.
  • The original poster expresses gratitude for the solution and seeks further resources for learning FORTRAN.
  • Additional resources for learning FORTRAN are mentioned, including a guide to Fortran77 and articles on Fortran 90.

Areas of Agreement / Disagreement

Participants generally agree on the nature of the problem and the proposed solution, but there is some uncertainty regarding the behavior of different compilers, particularly gfortran versus others.

Contextual Notes

The discussion does not resolve the underlying behavior of the gfortran compiler compared to others, nor does it clarify the implications of using different file statuses in FORTRAN.

Who May Find This Useful

This discussion may be useful for individuals troubleshooting FORTRAN file handling issues, particularly those new to the language or using gfortran.

jasonbot
Messages
16
Reaction score
0
Hi,

I'm having trouble with some FORTRAN code I'm trying to use.

I receive an error:

Code:
At line 769 of file ../src/mpolar.f (unit = 11, file = 'polarx.387')
Fortran runtime error: Sequential READ or WRITE not allowed after EOF marker, possibly use REWIND or BACKSPACE

Upon inspection of the code the following appears:

Code:
  55 READ(LU11,END=60) DUMMY
      GO TO 55
C
C
C---- the polar dump file doesn't exist, so write new header
   56 CONTINUE
      WRITE(LU11) NAME, ' MSES   ', VERSION
      WRITE(LU11) MACHIN, REYNIN/1.0E6, ACRIT
      WRITE(LU11) IMATYP, IRETYP
      WRITE(LU11) NBL, II
      WRITE(LU11) (ILEB(N), ITEB(N), IIB(N), N=1, NBL)
      DO 59 N=1, NBL
        WRITE(LU11) (XB(IB,N), YB(IB,N), IB=1, IIB(N))
   59 CONTINUE
C
   60 CONTINUE
C
      CLOSE(LU11)
      RETURN
      END ! PXINIT

as far as I understand the file is closing before it is written to. Upon inspection of the file system the file does get created but it is empty.

Could someone possibly explain to me what the code is trying to do and possibly suggest a solution?

I'm new to fortran code so I don't understand the syntax. Is there a good online manual for fortran?
 
Technology news on Phys.org
Post your complete code. That may get you more suggestions. It's hard to follow what the routine is trying to do with just this snippet of code.
 
I have attached the file that is giving me problems. The file is compiled with a makefile
 

Attachments

The file is open with status='unknown', such that if the file does not exist, it is created. There is then a read attempt, which in your case reaches the end of the file because it has just been created by the open statement and is empty. This is followed by a write to the file (since line 769 can only be reached if the file did not previously exist), where the program complains that it can't write since the file pointer is now beyond the EOF marker.

This is a strange behavior, probably a quirk of gcc, as a Fortran program would normally be expected to point just before the EOF marker when the EOF is reached. (I've tried with another compiler and the program executed normally.) To remedy the situation, just add the following line right after 56 CONTINUE:

REWIND(LU11)
 
  • Like
Likes   Reactions: 1 person
DrClaude said:
The file is open with status='unknown', such that if the file does not exist, it is created. There is then a read attempt, which in your case reaches the end of the file because it has just been created by the open statement and is empty. This is followed by a write to the file (since line 769 can only be reached if the file did not previously exist), where the program complains that it can't write since the file pointer is now beyond the EOF marker.

This is a strange behavior, probably a quirk of gcc, as a Fortran program would normally be expected to point just before the EOF marker when the EOF is reached. (I've tried with another compiler and the program executed normally.) To remedy the situation, just add the following line right after 56 CONTINUE:

REWIND(LU11)

Thank you so much! It seems to be working! I guess the error shouldve been self explanatory but since I have no working knowledge of fortran I didnt know the syntax that I needed to get the programme working.

Just to add, however, I was using gfortran not gcc. I don't know if that made a difference?

As I asked before, what is a good online resource to learn fortran?
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
9K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
9K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 14 ·
Replies
14
Views
2K