Fortran 2003-2008中是否有GETCWD()的替代方法 [英] Is there an alternative to GETCWD() in Fortran 2003-2008
问题描述
GNU Fortran编译器的GNU扩展提供了子例程GETCWD()
,该子例程很好地获取了当前的工作目录.但是,我的代码也必须可移植到ifort
和nagfor
编译器中,并且使用F2003功能.
The GNU Extension to the GNU Fortran compiler provides the subroutine GETCWD()
that well, gets the current working directory. However, my code has to be portable to the ifort
and nagfor
compiler as well and I use F2003 features.
那么,对于F2003及更高版本,是否有GETCWD()
的替代品?
So, is there an alternative to GETCWD()
for F2003 and later?
我这里有标准,但是它相当大,我已经研究了一段时间了,没有发现任何有用的东西...
I have the standard here but it's quite sizeable and I've been going through it for a while now and haven't found anything useful...
推荐答案
您还可以使用ISO_C_Binding
并调用相应的C
函数:
You can also use the ISO_C_Binding
and call the corresponding C
functions:
cwd.c:
#ifdef _WIN32
/* Windows */
#include <direct.h>
#define GETCWD _getcwd
#else
/* Unix */
#include <unistd.h>
#define GETCWD getcwd
#endif
void getCurrentWorkDir( char *str, int *stat )
{
if ( GETCWD(str, sizeof(str)) == str ) {
*stat = 0;
} else {
*stat = 1;
}
}
test.F90:
program test
use ISO_C_Binding, only: C_CHAR, C_INT
interface
subroutine getCurrentWorkDir(str, stat) bind(C, name="getCurrentWorkDir")
use ISO_C_Binding, only: C_CHAR, C_INT
character(kind=C_CHAR),intent(out) :: str(*)
integer(C_INT),intent(out) :: stat
end subroutine
end interface
character(len=30) :: str
integer(C_INT) :: stat
str=''
call getCurrentWorkDir(str, stat)
print *, stat, trim(str)
end program
此代码对Windows和Unix衍生版本(Linux,OSX,BSD等)有效.
This code is valid for Windows and Unix-derivates (Linux, OSX, BSD, etc. )
这篇关于Fortran 2003-2008中是否有GETCWD()的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!