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

FORTRAN IO OPERATION

Madhubalan_R_
Beginner
815 Views

Hi all I am using the following code to create a new file PROGRAM TEST_IO INTEGER*4 UNIT,IOS OPEN (UNIT=119, FILE = '$HOME/compile.log', IOSTAT = IOS, - STATUS = 'NEW', RECL = 132, FORM = 'FORMATTED', - RECORDTYPE = 'VARIABLE', ORGANIZATION = 'SEQUENTIAL', - CARRIAGECONTROL = 'FORTRAN') PRINT *,IOS END PROGRAM Here HOME(/home/my_name) is an environment variable.When I execute this code I am always getting the IOS as 46.Without using a function to get the value of the environment variable, Can I solve this issue?

0 Kudos
1 Solution
mecej4
Honored Contributor III
815 Views

You should not expect Fortran (or C, etc.) to do symbol substitutions or globbing in the same manner that we expect the Linux shell to do. You can call a library function to obtain the values of environment variables. See https://software.intel.com/en-us/node/526226; you can do

USE IFPORT   

character(len=50) :: buf

...

call getenv('HOME',buf)
OPEN (UNIT=119, FILE = trim(buf)//'/compile.log', IOSTAT = IOS,
- STATUS = 'NEW', RECL = 132, FORM = 'FORMATTED',
- RECORDTYPE = 'VARIABLE', ORGANIZATION = 'SEQUENTIAL',
- CARRIAGECONTROL = 'FORTRAN')

...

 

View solution in original post

0 Kudos
4 Replies
mecej4
Honored Contributor III
816 Views

You should not expect Fortran (or C, etc.) to do symbol substitutions or globbing in the same manner that we expect the Linux shell to do. You can call a library function to obtain the values of environment variables. See https://software.intel.com/en-us/node/526226; you can do

USE IFPORT   

character(len=50) :: buf

...

call getenv('HOME',buf)
OPEN (UNIT=119, FILE = trim(buf)//'/compile.log', IOSTAT = IOS,
- STATUS = 'NEW', RECL = 132, FORM = 'FORMATTED',
- RECORDTYPE = 'VARIABLE', ORGANIZATION = 'SEQUENTIAL',
- CARRIAGECONTROL = 'FORTRAN')

...

 

0 Kudos
Madhubalan_R_
Beginner
815 Views
Thanks you so much.
0 Kudos
FortranFan
Honored Contributor III
815 Views

mecej4 wrote:

You should not expect Fortran (or C, etc.) to do symbol substitutions or globbing in the same manner that we expect the Linux shell to do. You can call a library function to obtain the values of environment variables. See https://software.intel.com/en-us/node/526226; you can do

USE IFPORT   

character(len=50) :: buf

...

call getenv('HOME',buf)
OPEN (UNIT=119, FILE = trim(buf)//'/compile.log', IOSTAT = IOS,
- STATUS = 'NEW', RECL = 132, FORM = 'FORMATTED',
- RECORDTYPE = 'VARIABLE', ORGANIZATION = 'SEQUENTIAL',
- CARRIAGECONTROL = 'FORTRAN')

...

 

Standard Fortran subroutine (i.e., intrinsic procedure) GET_ENVIRONMENT_VARIABLE would now be preferable over an Intel extension of getenv:

See https://software.intel.com/en-us/node/580640.

With GET_ENVIRONMENT_VARIABLE, one does not need to include "USE IFPORT" statement.

 

0 Kudos
jimdempseyatthecove
Honored Contributor III
815 Views

One caveat on the suggestion is if the environment variable 'HOME' is not specified, then the filename '/compile.log' places the log file in the root directory. It might be better to test buf for all blanks and/or with trailing '/'.

BUF = ADJUSTL(BUF)
IF(BUF.EQ.' ') THEN
  BUF = './'
ELSE
  IF(BUF(LEN_TRIM(BUF):LEN_TRIM(BUF)) .NE. '/') BUF = TRIM(BUF)//'/'
ENDIF

Consider adjusting the above to handle '\' as separator.

Jim Dempsey

0 Kudos
Reply