如何从 Force Fortran 2.0 创建新目录

How to create a new directory from Force Fortran 2.0

我需要根据我的代码创建一个新目录,以便能够向其中写入数据文件。

我正在使用 Force Fortran 2.0 来自 Windows 8 我也想知道这种语法是否会有所不同由于 front/backslash 问题,从一个操作系统到另一个操作系统。

Force Fortran 使用较旧的编译器(g77g95gfortran [未知版本]),因此我将提供 system 的解决方案。对于支持它的编译器,最好使用符合标准的EXECUTE_COMMAND_LINE

您可以简单地使用 mkdir,它存在于 Windows 和 Unix 机器上。默认情况下,mkdir 在 Windows 上创建文件夹和(不存在的)父文件夹。这必须在 Unix 上明确给出 (-p)。使用 system 你可以从 Fortran 中执行:

program test
  implicit none
#ifdef _WIN32
  character(len=*),parameter :: MKDIR = 'mkdir '
  !                                           ^
  !                    The blank is intentional! 
#else
  character(len=*),parameter :: MKDIR = 'mkdir -p '
  !                                              ^
  !                       The blank is intentional! 
#endif
  integer :: stat

  stat = system( MKDIR // 'testFolder' )
  if ( stat /= 0 ) then
    print *, 'mkdir: failed to create folder! '
  endif
end program

您仍然需要创建一个例程来处理正确的文件夹定界符,这里是一个快速且简单的示例:

module conv_mod
contains
  function conv2win(str) result(res)
    implicit none
    character(len=*),intent(in) :: str
    character(len=len(str))     :: res
    integer                     :: i

    res = str
    do i=1,len(res)
      if ( res(i:i) == '/' ) res(i:i) = '\'
    enddo ! i
  end function

  function conv2unix(str) result(res)
    implicit none
    character(len=*),intent(in) :: str
    character(len=len(str))     :: res
    integer                     :: i

    res = str
    do i=1,len(res)
      if ( res(i:i) == '\' ) res(i:i) = '/'
    enddo ! i
  end function
end module

program conv
  use conv_mod
  print *,conv2win('some/path')
  print *,conv2win('some\path')
  print *,conv2unix('some\path')
end program

这不会处理 C:\ 之类的事情,但是...正如@VladimirF 指出的那样,您也可以在 Windows 中使用 /。在 Unix 中,您仍然需要将反斜杠转换为 /