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

open(newunit) in runtime library

lauhwa
Novice
597 Views

I write a new function as follow 

integer function openf(c) result(i)
!DEC$ ATTRIBUTES DLLEXPORT :: openf
character(len=*)::c
open(11,file=c)
!open(newunit=i,file=c)
i=11
end function

 

then I compile it into runtime library, then I call "openf" by

i = openf("ccc.txt")
inquire(i,exist=j)
write(*,*)j
inquire(file="ccc.txt",exist=j)
write(*,*)j

 

it will work normally and return both True

 

while when I use the parameter "newunit", as 

integer function openf(c) result(i)
!DEC$ ATTRIBUTES DLLEXPORT :: openf
character(len=*)::c
open(newunit=i,file=c)
end function

Then, it failed to open the file and the first inquire return false.

 

Is this a reasonable?

0 Kudos
3 Replies
jimdempseyatthecove
Honored Contributor III
571 Views

When using the "result(i)" on function declaration, you do not also attribute the function with return type "integer" ("integer function ...").

I am surprised a compiler error message did not report. Try:

  function openf(c) result(i)
    !DEC$ ATTRIBUTES DLLEXPORT :: openf
    character(len=*)::c
    open(newunit=i,file=c)
  end function

Additionally, use implicit none and declare all the dummy arguments

  function openf(c) result(i)
    !DEC$ ATTRIBUTES DLLEXPORT :: openf
    implicit none
    integer :: i  character(len=*)::c
    open(newunit=i,file=c)
  end function

 

Jim Dempsey

0 Kudos
lauhwa
Novice
470 Views

I filled to replay the question until I find a VPN. 

When I compile them with the same options( both debug or release, multithread dll), it work fine.

0 Kudos
mecej4
Honored Contributor III
550 Views

It is not clear if, in the scope containing the statement "i = openf("ccc.txt")", the identifier OPENF if of type INTEGER. WIth default typing, OPENF is of type default real, so the assignment statement would probably set i = 0 as a result of erroneous type conversion.

Please check and correct if needed.

0 Kudos
Reply