Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
New Contributor II
13 Views

Simple Question

if a file is located in C:\Users\macne\Strand7

how do  I create the string to open the file called a.txt from 

C:\Users\macne\Documents\Visual Studio 2017\Projects\Program102 - RungeKutta\Strand

I have not done this in such a long time I forget and the manual has me confused

 

0 Kudos
9 Replies
Highlighted
Black Belt Retired Employee
13 Views

There are a couple of possibilities. In Intel Fortran, you can use DEFAULTFILE= to specify the directory the file is in and then FILE='a.txt'. Otherwise just concatenate the directory name with the file name and open that. It doesn't matter where the program is.

0 Kudos
Highlighted
13 Views

integer, parameter :: YourMaxPath=512 ! your call as to value
character(len=YourMaxPath) :: rootPath
...
rootPath = "C:\Users\macne\Documents\Visual Studio 2017\Projects\Program102 - RungeKutta\" ! you fill in somehow
...
OPEN(YourUnit,file=rootPath//"Strand",...)

Jim Dempsey

0 Kudos
Highlighted
New Contributor II
13 Views

Jim:

Thanks, this is a call to a STRAND7 API that has a somewhat weird format. 

I used trim(xx)//trim(yy) to create the full path, but had to put in the // for each directory. 

John

0 Kudos
Highlighted
Valued Contributor III
13 Views

Nichols, John wrote:

if a file is located in C:\Users\macne\Strand7

how do  I create the string to open the file called a.txt from 

C:\Users\macne\Documents\Visual Studio 2017\Projects\Program102 - RungeKutta\Strand

I have not done this in such a long time I forget and the manual has me confused

An option you can consider is to add a suitably named environment variable to your system for the file you need to work with in your Fortran code.  You can then retrieve the value of the variable which can be the file name using (nonstandard) GETENV portability subroutine offered to you by Intel: https://software.intel.com/en-us/fortran-compiler-developer-guide-and-reference-getenv.

Note the current Fortran standard, Fortran 2018, offers an intrinsic procedure named GET_ENVIRONMENT_VARIABLE and the description for it in the standard lists an example which is pretty much the same as your need:

39   16.9.84 GET_ENVIRONMENT_VARIABLE (NAME [, VALUE, LENGTH,
STATUS, TRIM_NAME, ERRMSG])

40 1 Description. Get environment variable.
41 2 Class. Subroutine.

42 3 Arguments.
43 NAME shall be a default character scalar. It is an INTENT (IN) argument. The interpretation of case is
44 processor dependent.

1 VALUE (optional) a default character scalar. It is an INTENT (OUT) argument. It is assigned the value
2 of the environment variable specified by NAME. VALUE is assigned all blanks if the environment
3 variable does not exist or does not have a value, or if the processor does not support environment
4 variables.

5 LENGTH (optional) shall be a scalar of type integer with a decimal exponent range of at least four. It is an
6 INTENT (OUT) argument. If the specified environment variable exists and has a value, LENGTH
7 is assigned the value of its length. Otherwise LENGTH is assigned the value zero.

8 STATUS (optional) shall be a scalar of type integer with a decimal exponent range of at least four. It is an
9 INTENT (OUT) argument. If the environment variable exists and either has no value, its value is
10 successfully assigned to VALUE, or the VALUE argument is not present, STATUS is assigned the
11 value zero. STATUS is assigned the value ?1 if the VALUE argument is present and has a length
12 less than the significant length of the environment variable. It is assigned the value 1 if the specified
13 environment variable does not exist, or 2 if the processor does not support environment variables.
14 Processor-dependent values greater than 2 may be assigned for other error conditions.

15 TRIM_NAME (optional) shall be a logical scalar. It is an INTENT (IN) argument. If TRIM_NAME is present
16 with the value false then trailing blanks in NAME are considered significant if the processor sup17
ports trailing blanks in environment variable names. Otherwise trailing blanks in NAME are not
18 considered part of the environment variable’s name.

19 ERRMSG (optional) shall be a default character scalar. It is an INTENT (INOUT) argument. It is assigned
20 a processor-dependent explanatory message if the optional argument STATUS is, or would be if
21 present, assigned a positive value. Otherwise, it is unchanged.

22 4 It is processor dependent whether an environment variable that exists on an image also exists on another image,
23 and if it does exist on both images, whether the values are the same or different.

24 5 Example. If the value of the environment variable DATAFILE is datafile.dat, executing the statement sequence
25 below will assign the value ’datafile.dat’ to FILENAME.
CHARACTER(:),ALLOCATABLE :: FILENAME
INTEGER :: NAMELEN
CALL GET_ENVIRONMENT_VARIABLE ("DATAFILE", LENGTH=NAMELEN)
IF (LENGTH>0) THEN
ALLOCATE(CHARACTER(LENGTH) :: FILENAME)
CALL GET_ENVIRONMENT_VARIABLE("DATAFILE", FILENAME)
END IF

 

0 Kudos
Highlighted
Valued Contributor III
13 Views

By the way, I think the Example given in the standard has a typo, the 4th and 5th lines should reference NAMELEN instead of LENGTH.

0 Kudos
Highlighted
Valued Contributor III
13 Views

FortranFan wrote:

..

Note the current Fortran standard, Fortran 2018, offers an intrinsic procedure named GET_ENVIRONMENT_VARIABLE and the description for it in the standard lists an example which is pretty much the same as your need: ..

Also, if you're interested in making use of environment variables, you may try convincing Intel Fortran team to add the new standard intrinsic, GET_ENVIRONMENT_VARIABLE!  One would think it's a "low-hanging fruit" for the Intel Fortran team to grab in terms of additional Fortran 2018 features in their product.

Edited 1/17/2020, 09:30 EDT: thanks to Steve Lionel in Quote #8, it was my mistake for doing faulty "searches" in the PDF for Fortran 2008 standard and the Intel Fortran online documentation that I failed to locate GET_ENVIRONMENT_VARIABLE in either of them.

get_env.PNG

0 Kudos
Highlighted
Black Belt Retired Employee
13 Views

Eh? GET_ENVIRONMENT_VARIABLE isn't new in F2018 and has been supported in ifort for many years.

0 Kudos
Highlighted
Black Belt Retired Employee
13 Views

GET_ENVIRONMENT_VARIABLE was added in F2003.

0 Kudos
Highlighted
New Contributor II
13 Views

Thank you all - I use EV a lot in C# but have not used it in Fortran.  I will need to give it a go. 

 

0 Kudos