Fix file handling in Eatools_so.f90 to prevent “file already opened” error #9
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Error Explanation and Motivation
Problem:
At line 20 of
Eatools_so.f90(unit = 61), the program throws a runtime error:This happens because the code attempts to open the file
Cij.daton unit 61, but that file is already open on the same or a different Fortran unit without being closed first. Fortran forbids opening the same file multiple times simultaneously without closing it, causing the program to crash.Additionally, the original code had several issues:
inquire(unit=61, opened=isOpen)whereisOpenwas undeclared or incorrectly typed.close(file="Cij.dat")— Fortrancloseexpects a unit number, not a filename.What this fix improves:
Safe File Handling:
Uses
inquire(file="Cij.dat", opened=isOpen)to check if the file is already open (independent of unit). If open, it safely closes the file on the known unit (61) before reopening, avoiding multiple opens.Error Checking:
Adds
iostat=iosto theopenstatement to detect failures. If opening fails, the program prints a clear error message and exits gracefully.Correct Syntax and Declarations:
Fixes incorrect
closesyntax to use unit numbers instead of filenames.Declares variables
isOpen(logical) andios(integer) properly.Improved Robustness:
Prevents runtime crashes caused by file access conflicts.
Helps users quickly identify missing or inaccessible input files.
Makes the code more portable and maintainable.
Summary:
This pull request fixes file handling in
Eatools_so.f90to avoid the “file already opened” runtime error by adding proper file open checks, correct closing, error handling, and variable declarations. The changes improve program stability, prevent crashes, and provide clearer diagnostics for file-related issues.