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

  • Fortran
  • Thread starter bruhan
  • Start date
  • #1
5
0

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:

Answers and Replies

  • #2
chiro
Science Advisor
4,790
132


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.
 
  • #3
5
0
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)
 
  • #4
chiro
Science Advisor
4,790
132
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.
 
  • #5
fluidistic
Gold Member
3,674
112
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.
 
  • #6
SteamKing
Staff Emeritus
Science Advisor
Homework Helper
12,796
1,667
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.
 
  • #7
5
0
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
 
  • #8
5
0
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?
 
  • #9
5
0
What is weird is that the program runs with no changes at all on a PC (friends told me)!
 
  • #10
SteamKing
Staff Emeritus
Science Advisor
Homework Helper
12,796
1,667
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
103
0
Try open(unit=7, file='group.dat', form='formatted', status='old')
 

Related Threads on Fortran program can't find input file (original title: No Clue)

Replies
9
Views
10K
  • Last Post
Replies
1
Views
3K
Replies
3
Views
776
Replies
2
Views
12K
Replies
1
Views
1K
Replies
11
Views
6K
Replies
4
Views
2K
Replies
5
Views
2K
Replies
5
Views
5K
Top