Home > Archive > Fortran > November 2004 > Find Current Working Directory
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Find Current Working Directory
|
|
|
| I am trying to call a Fortran program (say c:\fortran\test.exe)
from another program at a different location (say c:\)
Within the Fortran program, I will need to open some other files stored
in c:\fortran to retrieve data.
How do I locate the current directory where the Fortran program is
stored? Without having this information, the program will keep looking
at c:\ instead of c:\fortran for the files that I want to access.
Help is greatly appreciated.
| |
| Ken Plotkin 2004-11-04, 3:57 am |
| On 3 Nov 2004 16:02:20 -0800, semusemu@gmail.com (Sam) wrote:
>I am trying to call a Fortran program (say c:\fortran\test.exe)
>from another program at a different location (say c:\)
>Within the Fortran program, I will need to open some other files stored
>in c:\fortran to retrieve data.
>How do I locate the current directory where the Fortran program is
>stored? Without having this information, the program will keep looking
>at c:\ instead of c:\fortran for the files that I want to access.
>Help is greatly appreciated.
What compiler are you using? From the \, I see you're on a PC.
I've run into a similar problem in that the "working" directory for a
program depends on how it's started: command line, double click or
drag and drop. In CVF, getarg(0,...) gives the fully qualified path
to the program. I use
call getarg(0,foo)
ipath = index(foo,'',.true.)
mypath = foo(1:ipath)
to get where it's at.
If you're not using CVF, see if your compiler has an equivalent to
getarg, and if it will return the same info.
Ken Plotkin
| |
| Kevin G. Rhoads 2004-11-04, 3:58 pm |
| >In CVF, getarg(0,...) gives the fully qualified path
>to the program. I use
>
>call getarg(0,foo)
>ipath = index(foo,'',.true.)
>mypath = foo(1:ipath)
>
>to get where it's at.
>
>If you're not using CVF, see if your compiler has an equivalent to
>getarg, and if it will return the same info.
Most Fortran compilers targeting x86/Windows will have something
similar. However, in the unlikely event that you are developing
embedded control programs for embedded-DOS hardware in Fortran,
be aware that early versions of DOS do not provide the information
needed for the Fortran RTL to find the CWD and prepend it to the
EXE name as arg #0. IIRC DOS 3.1 or 3.2 began providing that,
and for anything later, this approach works well. Most DOS boxes
(under OS/2, NT, Linux/DOSEMU, SoftPC/SoftWindows) will provide
all the needed info also. It is only if you are running under
very old DOS versions MS 2.x or early ConcurrentDOS that this is
likely to be an issue.
| |
| meek@skyway.usask.ca 2004-11-04, 3:58 pm |
| In a previous article, Ken Plotkin <kplotkin@nospam-cox.net> wrote:
>On 3 Nov 2004 16:02:20 -0800, semusemu@gmail.com (Sam) wrote:
>
>
>What compiler are you using? From the \, I see you're on a PC.
>
>I've run into a similar problem in that the "working" directory for a
>program depends on how it's started: command line, double click or
>drag and drop. In CVF, getarg(0,...) gives the fully qualified path
>to the program. I use
>
>call getarg(0,foo)
>ipath = index(foo,'',.true.)
>mypath = foo(1:ipath)
>
>to get where it's at.
>
>If you're not using CVF, see if your compiler has an equivalent to
>getarg, and if it will return the same info.
>
>Ken Plotkin
>
WATCOM gives the full path.
Chris
| |
| beliavsky@aol.com 2004-11-04, 3:58 pm |
| semusemu@gmail.com (Sam) wrote in message news:<d9a31ee5.0411031602.29f8101@posting.google.com>...
> I am trying to call a Fortran program (say c:\fortran\test.exe)
> from another program at a different location (say c:\)
> Within the Fortran program, I will need to open some other files stored
> in c:\fortran to retrieve data.
> How do I locate the current directory where the Fortran program is
> stored? Without having this information, the program will keep looking
> at c:\ instead of c:\fortran for the files that I want to access.
> Help is greatly appreciated.
An alternative to knowing the current working directory is to specify
the path for needed files. In the code, I would set a parameter for
the directory of the files, say
character (len=*), parameter :: cdir = "c:\fortran\"
and then OPEN files using the full file name, for example
open (unit=20,file=cdir // some_name)
| |
| Catherine Rees Lay 2004-11-05, 8:56 am |
| In article <3064b51d.0411040648.79789af9@posting.google.com>,
beliavsky@aol.com writes
>semusemu@gmail.com (Sam) wrote in message
>news:<d9a31ee5.0411031602.29f8101@posting.google.com>...
>
>An alternative to knowing the current working directory is to specify
>the path for needed files. In the code, I would set a parameter for
>the directory of the files, say
>
>character (len=*), parameter :: cdir = "c:\fortran\"
>
>and then OPEN files using the full file name, for example
>
>open (unit=20,file=cdir // some_name)
Only really a possibility if the program is for your use only. Users
expect to be able to install a program wherever they want and still have
it work.
However, hardcoding a directory name relative to the executable's is
common and works wherever the executable is. For instance
reldir = '\data'
exedir = <call to function which gets executable's directory>
open(20,file=trim(exedir)//reldir//some_name)
I'd advise not trying to set/use the current working directory even
though it sounds like a good solution - it's always seemed rather
ill-defined to me and it's far too prone to go wrong if, for instance,
you run using a debugger.
Catherine.
--
Catherine Rees Lay
To email me, use my first name in front of the "at".
|
|
|
|
|