Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29274 Discussions

how to get the file path for Fortran code

carlos8410
Beginner
3,360 Views

Hi,

I'm using the TCL/TK code to call an execute file which is written by Fortran code.

I don't know how to exchange the data between these two languages. So I do it through the data file.

When the TCL/TK code generates a project, it will write data to a file with a specific extention inside the project folder. And it calls the Fortran code to read the file and do some calcation. The problem is how to assign the path of the data file to Fortran code. Because every time when a project is generated, it may have different path and different name. And the Fortran code needs to know the path of the project so it can read the data file inside the folder.

Is there any way to solve this problem? I don't have much experience in Fortran.

P.S. I'm using the Intel Fortran Compiler 10 (Windows).

0 Kudos
11 Replies
anthonyrichards
New Contributor III
3,360 Views
If you cannot touch the fortran code, then you are stuck I think. You have to copy the data file to where the Fortran executable is stored, or else copy the fortran executable to where the data file is located.
However, If you have the source code and can compile it, then you need to modify it to call the IVF routine(or in my case, the CVF routine GETARG)that gets the command line used to start the Fortran program. Then when you invoke the new program add the full-path datafile name as an argument on the command line. The program can then pick up the datafile and carry on as you want it to.
Quoting - carlos8410

Hi,

I'm using the TCL/TK code to call an execute file which is written by Fortran code.

I don't know how to exchange the data between these two languages. So I do it through the data file.

When the TCL/TK code generates a project, it will write data to a file with a specific extention inside the project folder. And it calls the Fortran code to read the file and do some calcation. The problem is how to assign the path of the data file to Fortran code. Because every time when a project is generated, it may have different path and different name. And the Fortran code needs to know the path of the project so it can read the data file inside the folder.

Is there any way to solve this problem? I don't have much experience in Fortran.

P.S. I'm using the Intel Fortran Compiler 10 (Windows).

0 Kudos
carlos8410
Beginner
3,360 Views
Yes, I can modify the Fortran code.

But when a project is generated, the user already specifies the path and the name before calling the fortran code. So if the user needs to input the path again on the command line, it is not very user friendly.

I hope to find some way that can pass the path to the fortran code automatically.


Quoting - carlos8410

Hi,

I'm using the TCL/TK code to call an execute file which is written by Fortran code.

I don't know how to exchange the data between these two languages. So I do it through the data file.

When the TCL/TK code generates a project, it will write data to a file with a specific extention inside the project folder. And it calls the Fortran code to read the file and do some calcation. The problem is how to assign the path of the data file to Fortran code. Because every time when a project is generated, it may have different path and different name. And the Fortran code needs to know the path of the project so it can read the data file inside the folder.

Is there any way to solve this problem? I don't have much experience in Fortran.

P.S. I'm using the Intel Fortran Compiler 10 (Windows).

0 Kudos
Steven_L_Intel1
Employee
3,360 Views

If what you want is to find the location of the running executable, you can do that. You can then use that to construct the path to the data file. First you call the Win32 API GetModuleHandle with an argument of NULL to mean the current running program, and then GetModuleFileName to get the name of that file.

However, if the application will always be run from within Visual Studio, you don't need any path at all, as the default is the project folder.

0 Kudos
anthonyrichards
New Contributor III
3,360 Views
You say the user 'already computes the path to the file', in which case it should be available to be used as an argument to the command that starts the executable, no?
Exactly how does the user start the executable?

0 Kudos
Paul_Curtis
Valued Contributor I
3,360 Views

If your program is invoked as "myprog.exe", then the following Windows API will recover the complete path to your executable:

[cpp]
CHARACTER(LEN=256) :: fpname
INTEGER :: nc

!   get local root path from opsys; nc returned as
!   stringlength of total path
nc = GetModuleFileName(GetModuleHandle('MYPROG.EXE'C),   &   
fpname, LEN(fpname)) [/cpp]


0 Kudos
carlos8410
Beginner
3,360 Views
what I want is not to find the location of the running executable, but let the Fortran code know the path of current project, so it can read the data from the data file through the path.

And the application is not run from within Visual Studio.


I try to pass the path of the file as an argument to the fortran code. But I can't because TCL/TK and Fortran can't pass value through argument.

I paste part of my fortran code below to make my question clear.

At the beginning of the code, I need to read data from the data file. And the file is located inside the project folder.
Like the bold line below gives the path of the file. And (D:System FileDesktopGIDm1.gid) is the project folder.

But with the specified path here, the code can only read data from this file. If the user create projects using different path or name, the code doesn't work. Because it can't find the correct data file by using the path below.


======================================
PROGRAM MAIN

IMPLICIT REAL*8 (A-H,O-Z)

ALLOCATABLE :: X(:), XBASE(:), XOPT(:), XNEW(:), GQ(:), D(:),
1 FVAL(:), PQ(:), HQ(:), VLAG(:), W(:), XPT(:,:), BMAT(:,:),
2 ZMAT(:,:)

OPEN ( 15, file='D:System FileDesktopGIDm1.gidinput.inp' )
READ (15, *) N,NPT,IPRINT,NPTM,NF,NP,NDIM,NH,IDZ,KOPT,KNEW,
1 RHO_FLAG,JUMP_FLAG,FLAG_310
======================================


Quoting - carlos8410

Hi,

I'm using the TCL/TK code to call an execute file which is written by Fortran code.

I don't know how to exchange the data between these two languages. So I do it through the data file.

When the TCL/TK code generates a project, it will write data to a file with a specific extention inside the project folder. And it calls the Fortran code to read the file and do some calcation. The problem is how to assign the path of the data file to Fortran code. Because every time when a project is generated, it may have different path and different name. And the Fortran code needs to know the path of the project so it can read the data file inside the folder.

Is there any way to solve this problem? I don't have much experience in Fortran.

P.S. I'm using the Intel Fortran Compiler 10 (Windows).

0 Kudos
Les_Neilson
Valued Contributor II
3,360 Views
Did you see the reply from Paul Curtis?
I don't know TCL/TK but at the point in the TCL/TK code that runs the fortran program you should be able to append the name of the data file.
e.g. in fortran you would write :
command = program_name//' '//file_name
iret=system(command)
I don't know what the TCL/TK code would look like.
Then in your Fortran code you wouldCALL Get_Command_Argument(1, ...)
which would give you the file name.
Les

0 Kudos
Les_Neilson
Valued Contributor II
3,360 Views
Quoting - Les Neilson
Did you see the reply from Paul Curtis?

Sorry, I meant Anthony Richards
Les

0 Kudos
anthonyrichards
New Contributor III
3,360 Views
Can TCL/TK create/modify a Windows Reistry entry? If so, then create a registry key and place the full path to the project there.
it is straightforward to modify the Fortran code to read the relevant registry key and obtain the path to the data file.
0 Kudos
anthonyrichards
New Contributor III
3,360 Views
Quoting - anthonyrichards
Can TCL/TK create/modify a Windows Reistry entry? If so, then create a registry key and place the full path to the project there.
it is straightforward to modify the Fortran code to read the relevant registry key and obtain the path to the data file.

Something like

package require registry
registrySet "HKEY_CLASSES_ROOT\TclScript\YourProgram\ProjectFolder" (arguments here) ?

0 Kudos
Steven_L_Intel1
Employee
3,360 Views

Carlos,

The program has no idea where the project folder is. There is no information available to it that would give that. All it knows is where the executable is. If that was created in a project, then it is likely that the project folder is one level up in the folder tree, which you can get by appending .. to the executable path (without the filename).

Note that if you call GetModuleHandle, you can pass NULL as the first argument instead of the executable name.

0 Kudos
Reply