Fortran program can't find input file

Click For Summary

Discussion Overview

The discussion revolves around a Fortran program encountering a runtime error when attempting to open a file for reading. Participants explore potential causes and solutions related to file existence, directory settings, and syntax issues within the Fortran code.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant reports a Fortran runtime error indicating that the specified file cannot be found, despite attempts to change the file extension.
  • Another participant suggests checking the file's location relative to the source code and the current directory settings.
  • Concerns are raised about whether the file exists and if it is open in another program, which could prevent access.
  • A participant mentions that changing the file extension to .txt allowed them to open the file, indicating possible issues with the original .DAT file.
  • There is a suggestion to provide the full path to the file in the OPEN statement, although this leads to a new error regarding an unterminated character constant.
  • Another participant proposes creating an empty file named "GPROP.DAT" to satisfy the STATUS='OLD' requirement or removing that status to create the file anew.
  • Discussion includes the need for continuation in the OPEN statement for long file names, which may relate to the unterminated character constant error.
  • One participant notes that the program runs without issues on a PC, raising questions about platform-specific behavior.
  • There is mention of syntax for line continuation in Fortran 77 and a request for clarification on the syntax for newer standards.

Areas of Agreement / Disagreement

Participants express various viewpoints on the causes of the error, with no consensus on a single solution. Multiple competing views on how to address the issue remain unresolved.

Contextual Notes

Limitations include uncertainty about the file's existence and the current directory settings, as well as unresolved syntax issues related to the Fortran version being used.

bruhan
Messages
5
Reaction score
0
My gfortran compiler gives me the following message when I try to open a file in order to read it:Fortran runtime error: No such file or directory
logout

The line in the program where this file is is written in this way:

OPEN(UNIT = 7, FILE = 'GPROP.DAT', STATUS = 'OLD')

I've changed the extension to .txt (Mac OSX) and removed it altogether, with no changes in the resulting message.

Any ideas as to what could be going on? Tks.
 
Last edited:
Technology news on Phys.org


bruhan said:
My gfortran compiler gives me the following message when I try to open a file in order to read it:


Fortran runtime error: No such file or directory
logout

The line in the program where this file is is written in this way:

OPEN(UNIT = 7, FILE = 'GPROP.DAT', STATUS = 'OLD')

I've changed the extension to .txt (Mac OSX) and removed it altogether, with no changes in the resulting message.

Any ideas as to what could be going on? Tks.

Where is the file in relation to the source code and directory of your compiler?

If the only error is related to opening that file, here are some things you should check

1) Is the file already open by another program, (especially with write/exclusive permissions)?
2) Is your current directory set to the directory the file is in?
3) I know this might seem absurd but anyway... does the file even exist?

If 1 is false and 3 checks out, my suggestion is to label the file explicitly by supplying the full file name path. You wouldn't do this kind of thing normally, but then again you wouldn't normally hard-code string data like this either.
 
Dear Chiro, thanks for your reply.

The file is located in the same directory as the source code.
1. How can I check this out?
2. No, but I drag and drop the source code to the current directory, so that the full path of my source file is called.
3. I had my doubts. When I tried to open this .DAT file, it didn't open. It was just when I changed it to .txt that I succeeded in opening it and there is organized data there.

Chiro, this is what I get when giving the full path of the file whilst opening it:

gfortran -o /Users/brunohannud/Documents/Doutorado/ExecutaveisTurns/HPFLAME /Users/brunohannud/Documents/Doutorado/ExecutaveisTurns/HPFLAME.FOR
/Users/brunohannud/Documents/Doutorado/ExecutaveisTurns/HPFLAME.FOR:159.28:

OPEN(UNIT = 7, FILE = '/Users/brunohannud/Documents/Doutorado/Exec
1
Error: Unterminated character constant beginning at (1)
 
bruhan said:
Dear Chiro, thanks for your reply.

The file is located in the same directory as the source code.
1. How can I check this out?
2. No, but I drag and drop the source code to the current directory, so that the full path of my source file is called.
3. I had my doubts. When I tried to open this .DAT file, it didn't open. It was just when I changed it to .txt that I succeeded in opening it and there is organized data there.

Chiro, this is what I get when giving the full path of the file whilst opening it:

gfortran -o /Users/brunohannud/Documents/Doutorado/ExecutaveisTurns/HPFLAME /Users/brunohannud/Documents/Doutorado/ExecutaveisTurns/HPFLAME.FOR
/Users/brunohannud/Documents/Doutorado/ExecutaveisTurns/HPFLAME.FOR:159.28:

OPEN(UNIT = 7, FILE = '/Users/brunohannud/Documents/Doutorado/Exec
1
Error: Unterminated character constant beginning at (1)

The way fopen (or your OS open file routine) works is by using the "current directory" which is a variable stored for each process. If you omit a directory, then the OS just uses the current directory for that process. It is initialized to the directory that your .EXE (or linux equivalent) is in.

As for the error for unterminated character, maybe its because you haven't put a ' at the end of the string. I don't know fortran at all, but I have enough experience in other languages to realize that error is probably a simple syntax error like not terminating string literals.

If this code is run in an interpreted environment (as in an IDE and is not compiled to native code), then your best bet is to put your file where the IDE exe is.

My advice to find out what the "current directory" is, is to use some kind of directory command that gives you the current directory. I know QBASIC definitely has it, so I'm sure there is a command in FORTRAN.
 
I have an idea about what the problem is.
If you use
Code:
STATUS = 'OLD'
then the file must previously exist. Just create an empty file called "GPROP.DAT".
Now compile and execute your program. This should work.
Another option would be to remove the "status='old'" part. Compile the program so that it creates the file gprop.dat. Modify once again your code adding the "status='old'" and you're done.
 
If you are going to use long file names which contain multiple sub-directory references, the your OPEN statement will probably require continuation onto additional lines in the source code. That is the source of the 'Unterminated character constant' error.

Existing files have STATUS = 'OLD'. To open a new file, STATUS = 'NEW' instead.

To check whether a file is present, use the INQUIRE command before using OPEN.
 
Dear Fluidistic, I'm not sure I understand your instructions. I can create an empty file, ok, but I need to use the data in the original GPROP.DAT file. Please clarify what you mean.

Still, if I remove the SATATUS='OLD' part I get the folllowing:

ld: in /Users/brunohannud/Documents/Doutorado/ExecutaveisTurns/HPFLAME, can't link with a main executable
collect2: ld returned 1 exit status
 
SteamKing said:
If you are going to use long file names which contain multiple sub-directory references, the your OPEN statement will probably require continuation onto additional lines in the source code. That is the source of the 'Unterminated character constant' error.

Existing files have STATUS = 'OLD'. To open a new file, STATUS = 'NEW' instead.

To check whether a file is present, use the INQUIRE command before using OPEN.

Dear SteamKing, sorry for the question but what syntax do I use to make the continuation onto additional lines?
 
What is weird is that the program runs with no changes at all on a PC (friends told me)!
 
  • #10
In Fortran 77, statement lines were continued by putting a character (usually a '1' or a '+' in column 6 of each line after the initial line, to signify continuation. For the newer Fortran 90 and beyond compilers, consult the documentation, because of changes to the standard (I'm not as familiar with the newer standards).
 
  • #11
Try open(unit=7, file='group.dat', form='formatted', status='old')
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 12 ·
Replies
12
Views
11K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
8K