8

Fortan90 を使用してディレクトリが存在することを確認しようとしています。私が見つけたさまざまなサイトで:

logical :: dir_e
inquire(file='./docs/.', exist=dir_e)

if ( dir_e ) then
  write(*,*) "dir exists!"
else
  ! workaround: it calls an extern program...
  call system('mkdir docs')
end if

ただし、ディレクトリが存在するかどうかをinquire返し、このコードを2回実行するとエラーメッセージが表示されますFalse

ディレクトリを作成できません。ファイルは既に存在します

私が使用する場合:

inquire(file='./docs/test', exist=dir_e)

既存のファイル テストでは、 をinquire返しますtrue

ディレクトリの存在を確認するにはどうすればよいですか? ubuntu 11.04 と ifort コンパイラを使用しています。

4

7 に答える 7

7

Fortran 標準 95、2003、および 2008 では、inquire がディレクトリをどのように処理するかについて指定されていません。Linux での私の経験から、gfortran はそれらをファイルとして扱いますが、ifort は扱いません。ディレクトリ ステートメントは ifort 独自の機能であるため、避ける必要があります。

最も安全なのは、上記のディレクトリ内のファイルをテストすることです。

于 2014-02-18T08:54:11.467 に答える
5

以下が機能するはずです。

INQUIRE (DIRECTORY=dir, EXIST=ex [, DIRSPEC=dirspec] [, ERR=label] [, IOSTAT=i-var] )

このマシンには ifort がないため、テストできません。

補遺: 最初に投稿されたコードは gfortran で動作します。このDIRECTORYステートメントは ifort では機能しますが、gfortran では機能しません。

詳細については、次を確認してください: http://software.intel.com/sites/products/documentation/hpc/compilerpro/en-us/fortran/win/compiler_f/lref_for/source_files/rfinquir.htm#rfinquir

于 2012-03-01T20:16:28.420 に答える
3

ほとんどの場合、ディレクトリが存在するかどうかを確認して、そこに何かを書き込みます。私がすることは、ディレクトリを作成することだけです。すでに存在する場合は問題ありません。

     CALL system("mkdir video")
     CALL chdir("video")
     CALL getcwd(path)
于 2014-02-27T20:52:58.027 に答える
2

C ルーチンを使用してファイルをテストできます。

C側(Win32とLinux 32/64ではifortとgfortranでOK)

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#if defined(_WIN32) && defined(__INTEL_COMPILER)
#  include "dirent_windows.h"
#else
#  include <dirent.h>
#endif

void file_info(const char*filename,int*mode,int*exist,int*time){
  int k;
  struct stat buf;
  k=stat(filename,&buf);
  if(k != 0) {
    *mode=0;
    *exist=0;
    *time=0;
  }else{
    *mode=buf.st_mode;
    if(*mode == 0) *exist=0; else *exist=1;
    *time=buf.st_mtime;
  }
}

Fortran 側:

MODULE file

  USE iso_c_binding

  INTERFACE
    SUBROUTINE file_info(filename,mode,exist,time) BIND(C,name="file_info")
      USE iso_c_binding
      CHARACTER(kind=C_CHAR),INTENT(in) :: filename(*)
      INTEGER(C_INT),INTENT(out) :: mode,exist,time
    END SUBROUTINE
  END INTERFACE

END MODULE

Fortran ルーチンでの使用方法:

..
use file
use iso_c_binding
...
integer(c_int) :: mode,exist,time
...
call file_info("./docs"//char(0),mode,exist,time)

利点 : あらゆる種類のファイルに対して機能し、モード (読み取り/書き込み/実行許可) や作成時間などの追加情報を提供します。

于 2015-08-07T09:00:03.947 に答える
0

私も同じ問題を抱えていました。これを行うコンパイラに依存しない方法が必要な場合は、ディレクトリ内の小さなファイルを開いてみることができます。open ステートメントを使用すると、open ステートメントが失敗した場合に、コードを特定の行 (err= で指定) にジャンプできます。

! Tests whether the directory exists
subroutine checkdir(dir)
       implicit none
       character(len=*), intent(in) :: dir
       integer :: unitno

       ! Test whether the directory exists
       open(newunit=unitno,file=trim(dir)//'deleteme.txt',status='replace',err=1234)
       close (unitno)
       return

       ! If doesn't exist, end gracefully
1234   write(*,*) 'Data directory, '//trim(dir)//' does not exist or could not write there!'
       STOP

end subroutine

使用している OS に応じて、"dir" の末尾に "/" または "\" があると想定されるため、これは絶対確実ではないことに注意してください。

于 2015-06-11T18:00:01.390 に答える