Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Jun 25, 2011 #1
    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

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


    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: Jun 26, 2011
  2. jcsd
  3. Jun 25, 2011 #2


    User Avatar
    Science Advisor

    Re: No Clue

    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.
  4. Jun 26, 2011 #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

    OPEN(UNIT = 7, FILE = '/Users/brunohannud/Documents/Doutorado/Exec
    Error: Unterminated character constant beginning at (1)
  5. Jun 26, 2011 #4


    User Avatar
    Science Advisor

    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.
  6. Jun 26, 2011 #5


    User Avatar
    Gold Member

    I have an idea about what the problem is.
    If you use
    Code (Text):
    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.
  7. Jun 26, 2011 #6


    User Avatar
    Staff Emeritus
    Science Advisor
    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.
  8. Jun 26, 2011 #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
  9. Jun 26, 2011 #8
    Dear SteamKing, sorry for the question but what syntax do I use to make the continuation onto additional lines?
  10. Jun 26, 2011 #9
    What is weird is that the program runs with no changes at all on a PC (friends told me)!
  11. Jun 26, 2011 #10


    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    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).
  12. Jun 28, 2011 #11
    Try open(unit=7, file='group.dat', form='formatted', status='old')
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook