Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Объявления
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29285 Обсуждение

open(newunit) in runtime library

lauhwa
Новичок
952Просмотр.

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 баллов
3 Ответы
jimdempseyatthecove
Почетный участник III
926Просмотр.

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
Новичок
825Просмотр.

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
Почетный участник III
905Просмотр.

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.

Ответить