Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
The Intel sign-in experience has changed to support enhanced security controls. If you sign in, click here for more information.

open(newunit) in runtime library

lauhwa
Novice
296 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
Black Belt
270 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

lauhwa
Novice
169 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.

mecej4
Black Belt
249 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.

Reply