# Fortran program can't find input file (original title: No Clue)

• Fortran

## Main Question or Discussion Point

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:

Related Programming and Computer Science News on Phys.org
chiro

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.

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:

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

chiro

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:

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.

fluidistic
Gold Member
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.

SteamKing
Staff Emeritus
Homework Helper
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:

collect2: ld returned 1 exit status

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)!

SteamKing
Staff Emeritus