以非特定于编译器的方式在Fortran中更改目录 [英] Change of directory in Fortran in a non compiler-specific way
问题描述
我希望以Fortran 90代码更改工作目录。是否可以通过非特定于编译器的方式来执行此操作?这是我的代码:
I wish to change the working directory in a Fortran 90 code. Is it possible to do this in a non compiler-specific way? Here is my code:
program change_directory
integer :: ierr
call system("mkdir -p myfolder/")
!call system("cd myfolder/") !doesn't work
ierr = chdir("myfolder")
if (ierr.NE.0) then
write(*,'(A)') "warning: change of directory unsuccessful"
end if
open(unit=33,file="myfile.txt",iostat=ierr)
if (ierr.EQ.0) then
write(unit=33,fmt='(A)') "Test message"
close(unit=33)
end if
end program change_directory
显然,使用 cd myfolder /
在系统调用中不起作用。 英特尔参考说,我需要添加' use ifport
。不过, GCC参考中没有提及。省略 使用ifport
,我可以在 ifort
下编译上面的代码而没有任何麻烦。但是,当我将其放入时,它将无法使用gcc进行编译(因为gcc没有 ifport
模块),不仅如此,它也不会要么在Intel Fortran下编译-我遇到以下错误:
Clearly, using cd myfolder/
in a system call doesn't work. The Intel reference says I need to add 'use ifport
'. There's no such mention in the GCC reference, though. Leaving out 'use ifport
', I can compile the above code under ifort
without any trouble. When I put it in, however, it won't compile with gcc (because gcc doesn't have the ifport
module)--and not only that, it won't compile under Intel Fortran either--I'm getting the following error:
$ ifort change_dir.f90 -o change_dir
change_dir.f90(5): error #6552: The CALL statement is invoking a function subprogram as a subroutine. [SYSTEM]
call system("mkdir -p myfolder/")
---------^
compilation aborted for change_dir.f90 (code 1)
所以我的问题是:有更好的方法吗?我想让我的代码尽可能与编译器无关。此刻,我主要使用gfortran / ifort和mpif90 / mpiifort。
So my question is the following: is there a better way to do this? I'd like to keep my code as compiler-independent as possible. At the moment, I primary use gfortran/ifort and mpif90/mpiifort.
推荐答案
另请参见是否可以使用C语言更改目录?。您可以为 chdir()
创建自己的接口 POSIX调用独立于英特尔的界面。在Windows上类似。
See also Is there any way to change directory using C language? . You can make your own interface to the chdir()
POSIX call to be independent of the Intel's interface. On Windows it is similar.
module chdir_mod
implicit none
interface
integer function c_chdir(path) bind(C,name="chdir")
use iso_c_binding
character(kind=c_char) :: path(*)
end function
end interface
contains
subroutine chdir(path, err)
use iso_c_binding
character(*) :: path
integer, optional, intent(out) :: err
integer :: loc_err
loc_err = c_chdir(path//c_null_char)
if (present(err)) err = loc_err
end subroutine
end module chdir_mod
program test
use chdir_mod
call chdir("/")
call system("ls -l")
end
,并且在运行时
> gfortran chdir.f90
> ./a.out
celkem 120
drwxr-xr-x 2 root root 4096 15. říj 14.42 bin
drwxr-xr-x 5 root root 4096 15. říj 14.43 boot
...
在 ifort
上有效就像在 sunf90
上所做的一样。
On ifort
it works too as it does on sunf90
.
(注意:这取决于默认的字符
与 c_char
相同,这是一个很安全的假设,如果不是这种情况,编译器将抱怨并必须进行转换。 )
(Note: this relies on default character
being the same as c_char
. That is quite a safe assumption. If it is not the case the compiler will complain and a conversion has to be made.)
这篇关于以非特定于编译器的方式在Fortran中更改目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!