Fortran program can't find input file

In summary: This will print out the current working directory. 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
  • #1
bruhan
5
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
  • #2


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.
 
  • #3
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
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.
 
  • #5
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
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
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
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?
 
  • #9
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')
 

Related to Fortran program can't find input file

1. Why is my Fortran program unable to find the input file?

There could be several reasons for this issue. One possibility is that the input file is not located in the same directory as the Fortran program. Make sure that the file is in the correct location. Another reason could be that the file name or path is misspelled in the program. Check for any typos and correct them. Additionally, ensure that the file has the correct permissions for the program to access it.

2. How do I specify the path to the input file in my Fortran program?

To specify the path to the input file, you can use the "open" statement in your Fortran program. This statement allows you to specify the file name and path. Make sure to double-check the spelling and syntax of the statement.

3. Do I need to use a specific file format for my input file in Fortran?

Fortran programs can read various file formats, such as ASCII, binary, and formatted data. However, it is essential to specify the correct format in your program. If the input file is in a different format, you may encounter errors. You can use the "form" keyword in the "open" statement to specify the format of your input file.

4. Can I use relative paths for my input file in Fortran?

Yes, you can use relative paths for your input file in Fortran. Relative paths are specified based on the current working directory of the program. However, it is recommended to use absolute paths to avoid any confusion or errors.

5. How do I troubleshoot if my Fortran program still can't find the input file?

If your Fortran program still can't find the input file, try using a simple file with a short name and place it in the same directory as your program. This will help you identify if the issue is with the file itself or with the program. You can also check for any errors or warnings in the program output to determine the cause of the issue.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
12
Views
8K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
4
Views
797
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
Back
Top