×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Determining a running Fortran program's "residence directory"
2

Determining a running Fortran program's "residence directory"

Determining a running Fortran program's "residence directory"

(OP)
This is a repeat of a post I put on Eng-Tips about a fortnight ago.  See
https://www.eng-tips.com/viewthread.cfm?qid=493208
for that post and the replies it elicited.   It looks like I will not be able to achieve my aim in this matter, but I thought it worth trying this forum before I resign myself to this fate.

I am trying to get an old Fortran program to run on a modern Windows computer.  Progress is slow but steady.  However I have struck an "interesting" little problem.  My program's *.EXE file will reside in a directory that will be chosen by the end user, with different users choosing to store it in different places.  Users will run the program from a "Command Prompt" window, by typing its name and perhaps its residence directory.  Thus they might type:
» ProgName   (if the program resides in the current working directory or in a directory that is on the system's "path")
» D:\MySoftware\etc\etc\ProgName   (an absolute address for the residence directory)
» ..\ProgName   (if the program resides in the directory above the current working directory)

I am looking for a way for the program to find out the full name of its residence directory.  Gfortran has a subroutine GETCWD(cwd) that returns the full name of the directory the user is "sitting in", but I have been unable to find a way to obtain the program's residence directory.

Does anyone know of a way to achieve this?

RE: Determining a running Fortran program's "residence directory"

Hi,

To get current directory in windows you can use the windows CD command.
and to execute the windows command from the fortran program you can use SYSTEM() function/subroutine. Try something like this in your fortran program:

CODE

cmd_string = "cd > current_directory.txt"

cmd_rc = system (cmd_string)

if ( cmd_rc .ne. 0 ) stop 'system: Error !!!' 
then your program can read the current directory from the file current_directory.txt

RE: Determining a running Fortran program's "residence directory"

(OP)
Thanks, Mikrom.  However I think you have misunderstood what I am after.  I'm not after the "current directory".  I can get this from the semi-standard GETCWD subroutine, which returns the name of the directory my user is sitting in when he types the name of my .EXE program to run it.  However the .EXE program is not necessarily in that directory.  In fact it would be good practice (IMHO) for the program NOT to reside in that directory.  The program will be in some other directory, a directory that is on the system's "path".  In a networked environment, the program might even reside on a different computer, not just in a different directory.

RE: Determining a running Fortran program's "residence directory"

Hi Deniall,
I thought that the command returns the name of directory where the program resides. But now I tried it and - you are right - it returns the directory where the user who calls the program is sitting.

RE: Determining a running Fortran program's "residence directory"

Hi Deniall,

It seems that this problem is not solvable with pure fortran program.
But with fortran we can use C/C++.

I found here how to get the path of the running exe in C++:
https://stackoverflow.com/questions/143174/how-do-...
and here how to get from the given exe path only the directory path:
https://stackoverflow.com/questions/8518743/get-di...

Then based on the links above I wrote this code in C++:
get_exe_path.c

CODE

#include <string>
#include <windows.h>
#include <iostream>

std::string get_exe_path();
std::string get_directory(const std::string&);
extern "C" void getdir_();

std::string get_exe_path()
{
  char result[ MAX_PATH ];
  return std::string( result, GetModuleFileName( NULL, result, MAX_PATH ) );
}

std::string get_directory(const std::string &path)
{
    size_t found = path.find_last_of("/\\");
    return(path.substr(0, found));
}

extern "C" void getdir_() 
{
   std::cout << get_directory(get_exe_path()) << "\n";
}

/*
int main() {
  getdir_();
}
*/ 
You can uncomment main and run or debug the program, to see how it works.

Then I wrote this fortran code which calls the C code:
get_program_dir.f95

CODE

program get_program_dir
  write(*,*)"The program's residence directory:" 
  call getdir()
end program get_program_dir 

Then I compiled separately C++, fortran code and linked *.o files together into one executable

CODE

c:\00_mikrom\Work\fortran>g++ -c get_exe_path.c
c:\00_mikrom\Work\fortran>gfortran -c get_program_dir.f95
c:\00_mikrom\Work\fortran>gfortran get_exe_path.o get_program_dir.o -o get_program_dir -lstdc++ 

Now when I call it from the directory where I originally created it and where I'm sitting now I get this directory.
When I copy the executable get_program_dir.exe to my network drive into the folder U:\fortran\deniall\ and call it from my home directory I get the directory path where the fortran EXE currently resides:

CODE

c:\00_mikrom\Work\fortran>get_program_dir.exe
 The program's residence directory:
c:\00_mikrom\Work\fortran

c:\00_mikrom\Work\fortran>U:\fortran\deniall\get_program_dir.exe
 The program's residence directory:
U:\fortran\deniall 

So the program executable really prints the directory where it currently resides.
Try it.

RE: Determining a running Fortran program's "residence directory"

(OP)
Many thanks, Mikrom.  That looks like it is doing exactly what I want.

However I am currently on the outer edges of civilisation, doing farm labour to help a friend harvest his crop.  There is no internet, and I can only access the e-world using my mobile phone's feeble and intermittent mobile signal.  So I won't be able to try to implement your solution for another fortnight or so.

One further thing.  I don't have a C++ compiler, and I have never used any form of C before.  Any chance you could send me "get_exe_path.o" as an attachment?

Again, many thanks.

--------------

As a PS, allow me to explain why I want this capability.  The program I am working on produces a text file as its principal output.  It makes certain assumptions about how the user might want this file to be formatted:  page length; page width; standardised page heading information; output to be included; output to be explicitly excluded; etc.  The program also looks for a small "configuration file" that the user can create, via which some of these assumptions can be changed.  At present, the program looks for this configuration file in the current working directory only, where it will reside along with the user's input data file.  I envisage a scenario where a company that uses the program might decide upon a standardised configuration file, yet on some particular projects might wish to deviate from this.  The way I hope to allow this would have the company store its corporate configuration file alongside the .EXE file, both in some read-only directory somewhere on the corporate network.  If some specific project requires a slightly different configuration, then the actual users can create a different configuration file that they store in the directory that contains their data files, the directory FROM WHICH they run the .EXE file.

So when the program is run it looks first in the "current directory" for a configuration file, and if it finds one it uses it then gets on with the job.  If it doesn't find one there it looks in the directory in which the .EXE file resides, and uses that one.  If it cannot find one there it simply runs with its inbuilt default configuration.

Clear as mud, eh?

RE: Determining a running Fortran program's "residence directory"

Hi Deniall,

Here I uploaded the file get_exe_path.o as image:
https://res.cloudinary.com/engineering-com/raw/upload/v1649662694/tips/get_exe_path_grukgi.o
It was renamed by uploader, so after downloading, rename it to its original name.
Try to link it with the fortran code using the instructions I provided above.
Unfortunatelly if you have very different version of gfortran linking may not work.
I compiled it using gcc and gfortran versions 8.1.0 which are included in the Code::Blocks installation package codeblocks-20.03mingw-nosetup.zip available here: https://sourceforge.net/projects/codeblocks/files/...

RE: Determining a running Fortran program's "residence directory"

(OP)
Thanks (again), Mikrom.

I tried what you sent me, but the linking step gave an error:
get_exe_path.o: file not recognized: File format not recognized
So there must be some sort of incompatibility between the compiler and the C++ object file you uploaded for me.

I think I am using GCC version 5.1.0, although I am deducing this from the Fortran User Manual that came with the package.  I downloaded the compiler about a month ago, and merely selected what I thought looked the best package at the time.

I will try again in a couple of weeks, but I cannot justify putting a large amount of effort into achieving a feature that is really only peripheral to my program's main purpose.  Neither can I justify asking you for further assistance:  you have already been very generous with your time and your grey matter.

RE: Determining a running Fortran program's "residence directory"

on the command line type

CODE

gfortran --version 
and you will see what version you have.
Isn't gcc part of your gfortran package? Check it with:

CODE

gcc --version 


RE: Determining a running Fortran program's "residence directory"

Hi Deniall,
It seems to be very exciting. Say it finally: Have you tried it? Does it work or doesn't it?

RE: Determining a running Fortran program's "residence directory"

(OP)
Thanks for your persistence with this confused newbie, Mikrom.

I have tried the alternative version of get_exe_path.o that you sent me.  It seemed to get a bit further, but only a bit.  That --version trick you told me about informs me that both my GFortran and my GCC are version 6.3.0.

Here, in a Code Box, is the CMD-window dialogue between GFortran and me.  "Q:\BELF>" is the command prompt (with Q: being a logical drive mapping I use to lessen the amount of typing I need to do). You will notice that the problems seem to begin when GFortran takes offence with the "-lstdc++", saying that it "cannot be found".  So I removed it and tried again, but that seemed to make things worse.

CODE

Q:\BELF>Type get_program_dir.for
      Program get_program_dir
      write(*,*)"The program's residence directory:"
      call getdir()
      call exit
      end

Q:\BELF>GFortran  -c  get_program_dir.for

Q:\BELF>GFortran get_exe_path.o get_program_dir.o -o get_program_dir -lstdc++
d:/rmn/z_others/rmndev/gnu_fortran/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lstdc++
collect2.exe: error: ld returned 1 exit status

Q:\BELF>
Q:\BELF>REM  Try again with out the troublesome "-lstdc++" bit.

Q:\BELF>GFortran get_exe_path.o get_program_dir.o -o get_program_dir
get_exe_path.o:get_exe_path.c:(.text+0x12): undefined reference to `__gxx_personality_sj0'
get_exe_path.o:get_exe_path.c:(.text+0x40): undefined reference to `_Unwind_SjLj_Register'
get_exe_path.o:get_exe_path.c:(.text+0x4a): undefined reference to `std::allocator<char>::allocator()'
get_exe_path.o:get_exe_path.c:(.text+0x93): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, unsigned int, std::allocator<char> const&)'
   ...   h e a p s   m o r e   m e s s a g e s   o m i t t e d   ...
get_exe_path.o:get_exe_path.c:(.text+0x24f): undefined reference to `_Unwind_SjLj_Unregister'
get_exe_path.o:get_exe_path.c:(.text+0x26a): undefined reference to `std::ios_base::Init::~Init()'
get_exe_path.o:get_exe_path.c:(.text+0x28b): undefined reference to `std::ios_base::Init::Init()'
collect2.exe: error: ld returned 1 exit status

Q:\BELF> 

RE: Determining a running Fortran program's "residence directory"

(OP)
GFortran is still displeased with the "-lstdc++" bit, although I think the resulting blizzard of error messages is slightly different.  See below.

As I said in an earlier reply, this problem is not all that important to me in my overall scheme of things.  Both my program and I can live without this issue being solved.  I have already taken up a lot of your very helpful time, so please do not feel under any compunction to continue helping me.

CODE -->

Q:\BELF>

Q:\BELF>GFortran get_exe_path.o get_program_dir.o -o get_program_dir
get_exe_path.o:get_exe_path.c:(.text+0x10): undefined reference to `std::allocator<char>::allocator()'
get_exe_path.o:get_exe_path.c:(.text+0x4f): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, unsigned int, std::allocator<char> const&)'
get_exe_path.o:get_exe_path.c:(.text+0x5c): undefined reference to `std::allocator<char>::~allocator()'
get_exe_path.o:get_exe_path.c:(.text+0x6a): undefined reference to `std::allocator<char>::~allocator()'
get_exe_path.o:get_exe_path.c:(.text+0x9b): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::find_last_of(char const*, unsigned int) const'
get_exe_path.o:get_exe_path.c:(.text+0xc0): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::substr(unsigned int, unsigned int) const'
get_exe_path.o:get_exe_path.c:(.text+0xfa): undefined reference to `std::cout'
get_exe_path.o:get_exe_path.c:(.text+0xff): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
get_exe_path.o:get_exe_path.c:(.text+0x10f): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
get_exe_path.o:get_exe_path.c:(.text+0x119): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
get_exe_path.o:get_exe_path.c:(.text+0x123): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
get_exe_path.o:get_exe_path.c:(.text+0x131): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
get_exe_path.o:get_exe_path.c:(.text+0x13f): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
get_exe_path.o:get_exe_path.c:(.text+0x15f): undefined reference to `std::ios_base::Init::~Init()'
get_exe_path.o:get_exe_path.c:(.text+0x180): undefined reference to `std::ios_base::Init::Init()'
get_exe_path.o:get_exe_path.c:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'
collect2.exe: error: ld returned 1 exit status

Q:\BELF> 

RE: Determining a running Fortran program's "residence directory"

It must be linked with the option -lstdc++, otherwise it would not work.

Trying to use the option -lstdc++ you got this error

CODE

Q:\BELF>GFortran get_exe_path.o get_program_dir.o -o get_program_dir -lstdc++
d:/rmn/z_others/rmndev/gnu_fortran/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lstdc++
collect2.exe: error: ld returned 1 exit status 

It means that you don't have libstdc++ present in your fortran distribution in the directory
d:/rmn/z_others/rmndev/gnu_fortran/mingw/lib/gcc/mingw32/6.3.0/

I upload you two files from my MinGW 6.3.0 version: libstdc++.a and libstdc++.dll.a

Download both, copy them in your directory
d:/rmn/z_others/rmndev/gnu_fortran/mingw/lib/gcc/mingw32/6.3.0/
and try to link with the option -lstdc++.
If it does not help, you can delete both files.

https://res.cloudinary.com/engineering-com/raw/upload/v1649834138/tips/libstdc_ttdm4n.a

https://res.cloudinary.com/engineering-com/raw/upload/v1649834162/tips/libstdc_.dll_dkummw.a




RE: Determining a running Fortran program's "residence directory"

With my MinGW 6.3.0 i have this installer


If you have it too, you can install the c++ compiler additionally

RE: Determining a running Fortran program's "residence directory"

I thought about, why to use such a ballast as C++ for this simple task.
To get rid of libstdc++ i rewrote all from C++ in C:

get_exe_dir.c

CODE

/*
compile:
  gcc -c get_exe_dir.c
  gfortran -c get_program_dir.f95
  gfortran get_exe_dir.o get_program_dir.o -o get_program_dir
*/
#include <stdio.h>
#include <string.h>
#include <windows.h>

char *get_exe_dir(void);
void getdir_();

char *get_exe_dir()
{
    int exe_path_length, last_backslash;
    char *exe_path;
    // static does that the string will be permanently
    // allocated in the data segment of the program
    static char *result;

    // allocate memory for string
    exe_path = (char *) malloc(MAX_PATH * sizeof(char));

    exe_path_length = GetModuleFileName(NULL, exe_path, MAX_PATH);

    // find last backslash
    last_backslash = 0;
    for (int i = 0; i < exe_path_length && exe_path[i] != '\0'; i++) {
        if (exe_path[i] == '\\') {
            last_backslash = i;
        }
    }

    // allocate memory for resulting string
    result = (char *) malloc(last_backslash * sizeof(char));

    strncpy(result, exe_path, last_backslash);
    result[last_backslash] = '\0';

    return result;
}

void getdir_()
{
    printf("%s\n", get_exe_dir());
}

/*
int main()
{
    getdir_();
}
*/ 

The fortran source is the same - i changed only comment how to compile it

get_program_dir.f95

CODE

!compile:
!  gcc -c get_exe_dir.c
!  gfortran -c get_program_dir.f95
!  gfortran get_exe_dir.o get_program_dir.o -o get_program_dir
program get_program_dir
  write(*,*)"The program's residence directory:" 
  call getdir()
end program get_program_dir 

Now we can build the executable using these commands:

CODE

> gcc -c get_exe_dir.c
> gfortran -c get_program_dir.f95
> gfortran get_exe_dir.o get_program_dir.o -o get_program_dir 

The resulting exe has 40 kB, the previous exe which was build with C++ standard library has 380 kB.

Attached is the file get_exe_dir.o for the MinGW gcc/gfortran version 6.3.0
https://res.cloudinary.com/engineering-com/raw/upload/v1649847612/tips/get_exe_dir_qhuy7d.o

RE: Determining a running Fortran program's "residence directory"

Finally I found fortran solution (without need to program something in C) posted in comp.lang.fortran here:
https://groups.google.com/g/comp.lang.fortran/c/RT...

wintest.f95

CODE

module Win32
  use ISO_C_BINDING
  implicit none

  private

  public GetCurrentDirectory
  interface
    function GetCurrentDirectory(nBufferLength, lpBuffer) &
    bind(C,Name='GetCurrentDirectoryA')
      use ISO_C_BINDING
      implicit NONE
      !GCC$ ATTRIBUTES STDCALL :: GetCurrentDirectory
      integer(C_LONG) GetCurrentDirectory
      integer(C_LONG),value :: nBufferLength
      character(kind=C_CHAR) lpBuffer(*)
    end function GetCurrentDirectory
  end interface

  public GetModuleFileName
  interface
    function GetModuleFileName(hModule, lpFileName, nSize) &
    bind(C,Name='GetModuleFileNameA')
      use ISO_C_BINDING
      implicit NONE
      !GCC$ ATTRIBUTES STDCALL :: GetModuleFileName
      integer(C_LONG) GetModuleFileName
      integer(C_INTPTR_T), value :: hModule
      character(kind=C_CHAR) lpFileName(*)
      integer(C_LONG), value :: nSize
    end function GetModuleFileName
  end interface
end module Win32

program test
  use Win32
  use ISO_C_BINDING
  implicit none
  integer(C_LONG) b
  character StringA*(255)

  b = GetCurrentDirectory(len(StringA),StringA)
  print *,b
  print *,' Current Dir: '//StringA(1:b)

  b = GetModuleFileName(0_C_INTPTR_T,StringA,len(StringA))
  print *,b
  print *,' Current ExeName: '//StringA(1:b)
end program test 

After compilling it with

CODE

>gfortran wintest.f95 -o wintest 

it works



RE: Determining a running Fortran program's "residence directory"

(OP)
Mikrom.
I have successfully implemented your 13Apr22@11:02 approach, and it works exactly as you indicated.  I will not be able to explore it any further for a few days, because (as I said a few days ago) I am "on the outer edges of civilisation".  Once my walnut-picking sojourn is over I will get back to it, explore it further, and utilise it in the program I am developing.
Many, many, thanks for all your help.

RE: Determining a running Fortran program's "residence directory"

Hi Deniall,
So your problem has finally been solved. Wish you all the best.

RE: Determining a running Fortran program's "residence directory"

(OP)
While I was shoving thousands of walnuts into 10kg string bags my mind began to wander a bit.

Your get_exe_dir routine determines the program's "residence directory" and places the result on the screen.  But where I want it is inside my main program, so (as per your post on 09Apr22@07:24) I can use a small separate program via a SYSTEM call to run your routine and redirect its output to a separate file, then read that file from my main program, then delete the file.

This is fine, but a bit convoluted and requires an extra .EXE file in what I eventually distribute to my miniscule clientele.  Would it be possible to change get_exe_dir to make this a bit more straight forward.  Possible approaches might be:
»  Have get_exe_dir place the pathname directly into a file instead of shooting it to the screen. Any fixed file name would do.
»  Convert get_exe_dir to a subroutine callable directly from within the main program via a line CALL GET_DIR_EXE(RESIDENCE) where RESIDENCE is an appropriately sized CHARACTER variable that would contain the required information.
»  Convert get_exe_dir to a function which could be invoked via RESIDENCE=GET_DIR_EXE() or something similar.

RE: Determining a running Fortran program's "residence directory"

Hi Deniall,
It's clear that you need a fortran function which returns the EXE path, e.g.:
RESIDENCE = GET_DIR_EXE()
or a subroutine which sets the EXE path to the string, e.g.:
CALL GET_DIR_EXE(RESIDENCE)

But for the first step - i.e. a proof-of-concept-example it was simplest for me when I called the C-Function getdir() which calls get_exe_dir() and prints the path.

Then I thought about the second step - to find out, how to convert C-string which is the pointer on character to the fortran string. IMO it's not well documented, how to do it. Although I could read in the classic book by Kernighan and Ritchie how the C-strings works, I couldn't find nothing similar about Fortran-strings.
And you were on the very edge of civilization during your walnut-gathering sojourn, so I thought about waiting for you, that you first write if you even need such functionality.

But then, quite coincidentally, I have found the pure fortran solution, which I posted here on 13 Apr 22 11:45. Then I thought this is the best solution, because the fortran programmer doesn't need to bother with C.
Have you tried the fortran solution, which I posted on 13 Apr 22 11:45?

RE: Determining a running Fortran program's "residence directory"

(OP)
It's "true confession" time.  I did not try your 13Apr22 11:45 solution because I did not understand it.  (I cut my teeth on Fortran 4, then stepped up to Fortran 77, but then never moved any further.  So with that code you posted I did not even understand the syntax, let alone the logic.)

I will explore it when I get back to what we laughingly call "civilisation".  I fully expect that with a bit of experimentation I will be able to get it working.

Apologies and further thanks.

RE: Determining a running Fortran program's "residence directory"

Ok, try it, it's not so complicated.
Some years ago, I was also been a fan of Fortran77 only.
But then, to complete the tasks, I also recognized the merits of Forrtran 90 - namely, the modules. I'm sure you can handle it. It's not so difficult.

RE: Determining a running Fortran program's "residence directory"

(OP)
I am back in civilisation, and have successfully test-implemented an adaptation of your F95 solution.  I am posting it here so that it will be available to anyone else who might have this weird urge to determine a program's residence directory.

(1)  The F95 "subroutine":

CODE -->

module Win32
  use ISO_C_BINDING
  implicit none
  private
!
  public GetModuleFileName
  interface
    function GetModuleFileName(hModule, lpFileName, nSize) &
    bind(C,Name='GetModuleFileNameA')
      use ISO_C_BINDING
      implicit NONE
      !GCC$ ATTRIBUTES STDCALL :: GetModuleFileName
      integer(C_LONG) GetModuleFileName
      integer(C_INTPTR_T), value :: hModule
      character(kind=C_CHAR) lpFileName(*)
      integer(C_LONG), value :: nSize
    end function GetModuleFileName
  end interface
end module Win32
!
subroutine RESDIR(FilName,LenFilName)
  use Win32
  use ISO_C_BINDING
  implicit none
  integer(C_LONG) c
  character StringB*(255)
  integer(C_LONG) LenFilName
  character FilName*(255) 
  c = GetModuleFileName(0_C_INTPTR_T,StringB,len(StringB))
  FilName = StringB
  LenFilName = c
  return
end subroutine RESDIR 

(2)  The short F77 main program used to test the subroutine:

CODE -->

      PROGRAM MAINPROG
C
C     Written to test the Fortran-95 code that returns the
C     calling program's "residence directory".
C
C     Deliberately written in and compiled as Fortran-77.
C
      INTEGER LenFilName, LenResPathName
      CHARACTER*255 FilName, ResPathName
      CHARACTER*1 Dummy
C
      CALL RESDIR(FilName,LenFilName)
      WRITE(*,920)LenFilName, FilName(1:LenFilName)
  920 FORMAT(/' RUNNING FILE'/
     +        '   Length  = ',I3/
     +        '   Address = ',A)
C
      ResPathName = FilName
      LenResPathName = LenFilName
      DO 100 I = LenResPathName,1,-1
          IF(ResPathName(I:I).EQ.'\')THEN
              J = I - 1
              GO TO 400
          ENDIF
  100 CONTINUE
      DO 200 I = LenResPathName,1,-1
          IF(ResPathName(I:I).EQ.':')THEN
              J = I
              GO TO 400
          ENDIF
  200 CONTINUE
C
  400 LenResPathName = J
      WRITE(*,940)LenResPathName, ResPathName(1:LenResPathName)
  940 FORMAT(' RESIDENCE DIRECTORY'/
     +       '   Length  = ',I3/
     +       '   Address = ',A)
C
      WRITE(*,960,advance='no')
  960 FORMAT(/' Hit <Enter> to finish: ')
      READ(*,'(A1)')Dummy
C
      CALL EXIT
      END 

(3)  The GFortran commands to compile and link it all.
GFortran -c MainProg.for
GFortran -c FortSub.f95
GFortran  MainProg.o FortSub.o  -o MainProg

RE: Determining a running Fortran program's "residence directory"

Hi Deniall,
Nice work !

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close